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

Asked

Viewed 291 times

0

How can I make a custom alertDialog close after the user activates the gps? The dialog box tbm can not close while the gps condition is false, that is, while it is disabled alertDialog has to remain, but when active gps the 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();

1 answer

1

You must use the 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 the 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;
            }
        }  
}
  • Could you be a little clearer? Can’t apply my code. Still the same thing. The alert still continues even after activating. How do I close Alertdialog within this condition? I’m a beginner yet.. =(

  • The top part that spoke I understood, but the bottom part could not apply in my code.

  • Another question is also how can I always keep making the check whether the gps is activated or not? pq the user can enable and disable and the program does not do another check to know. some light? thanks in advance friend

Browser other questions tagged

You are not signed in. Login or sign up in order to post.