How do I close a dialog when the condition is true?

0

How can I make a custom alertDialog close after the user activates gps? The dialog box tbm can not close while the gps condition is false, ie while it is disabled the alertDialog has to remain, but when activated the gps dialog box still remains.

Follow my code:

LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
final boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);

if (!enabled) {

    LayoutInflater li = getLayoutInflater();
    View view = li.inflate(R.layout.alert_dialog__custom, null);

    final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    final AlertDialog alert = alertDialog.create();
    alertDialog.setCancelable(false);


    view.findViewById(R.id.btn_claro).setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            onStop();
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);

        }
    });

    view.findViewById(R.id.btn_depois).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            AlertSair();
        }
    });
    alert.dismiss();
    alertDialog.setView(view);
    alertDialog.show();

} onResume();
    
asked by anonymous 05.10.2016 / 22:08

1 answer

1

You should use startActivityForResult

example:

view.findViewById(R.id.btn_claro).setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        onStop();
        startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 1);
    }
});

And when the user activates GPS will return the result:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1) {
           switch (requestCode) {
              case 1:

               //aqui fecha o alerta                   

               break;
            }
        }  
}
    
05.10.2016 / 22:13