Android activate gps with AlertDialog: how to wait for the user to take action?

后端 未结 5 783
春和景丽
春和景丽 2020-12-17 20:18

I\'ve read different posts that there is no way to wait for the user to take an action when an AlertDialog is shown because it blocks the UI.

However,

5条回答
  •  猫巷女王i
    2020-12-17 20:55

    I managed to get this to work.I got inspired by this article here which i found on stackoverlfow. http://developmentality.wordpress.com/2009/10/31/android-dialog-box-tutorial/

    I basically moved the logic that i needed to execute on the "Yes" and "Cancel" buttons of the alert Dialog. After performing logic needed for Yes and Cancel buttons i start the asyncronous logic execution.

    Here's my code:

    public interface ICommand
    {
        void execute();
    }
    

    The two concrete Commands used for Enable Gps and Cancel buttons of the alert Dialog:

    public class CancelCommand implements ICommand
    {
        protected Activity m_activity;
    
        public CancelCommand(Activity activity)
        {
            m_activity = activity;
        }
    
        public void execute()
        {
            dialog.dismiss();
            //start asyncronous operation here
        }
    }
    
    
    public class EnableGpsCommand extends CancelCommand
    {
        public EnableGpsCommand( Activity activity) {
            super(activity);
        }
    
        public void execute()
        {
            // take the user to the phone gps settings and then start the asyncronous logic.
            m_activity.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
            super.execute();
        }
    }
    

    And now, from the Activity:

    //returns true if the GpsProviderIsDisabled
    //false otherwise
    private boolean EnableGPSIfPossible()
    {   
        final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
        if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            buildAlertMessageNoGps();
            return true;
        }
        return false;
    }
    
    
    private  void buildAlertMessageNoGps()
    {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Yout GPS seems to be disabled, do you want to enable it?")
            .setCancelable(false)
            .setPositiveButton("Yes", new CommandWrapper(new EnableGpsCommand(this)))
            .setNegativeButton("No", new CommandWrapper(new CancelCommand(this))); 
    
        final AlertDialog alert = builder.create();
        alert.show();
    }
    

    Now from the Activity OnCreate methodi just call:

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newfriendlist);
    
        InitializeComponents();
    
        if (!EnableGPSIfPossible())
        {
            //then gps is already enabled and we need to do StartFriendRetrievalAsync from here.
            //otherwise this code is being executed from the EnableGpsIfPossible() logic.
    
    
            //Asyncronous logic here.
        }
    }
    

    I really hope this will help you when you get stuck at this point :).

    Cheers

提交回复
热议问题