0

I have an alert dialog with a custom layout that when the gps or network are not enabled it will pop up so the user can enable them.

Here is the code:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // Get Location Manager and check for GPS & Network location services
    final LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View mView = getLayoutInflater().inflate(R.layout.activity_gps_alert_dialog, null);
        Button positiveBtn = (Button) mView.findViewById(R.id.positiveBtn);
        final Button negativeBtn = (Button) mView.findViewById(R.id.negativeBtn);
        builder.setView(mView);
        final AlertDialog dialog = builder.create();
        positiveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Show location settings when the user acknowledges the alert dialog
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);

            }
        });

        negativeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                System.exit(0);
            }
        });
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

The problem is that this popup dialog is on the screen even if the GPS and network are enabled and even if the user enables them through it the alertdialog still stays on the screen.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Alex
  • 1,816
  • 5
  • 23
  • 39

1 Answers1

0

You must close the dialog with dialog.didmiss() after the user presses the positive button:

    positiveBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Show location settings when the user acknowledges the alert dialog
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
            dialog.didmiss()
        }
    });

For the other part, that the dialog pops up even if gps and network is enabled see these posts: isProviderEnabled(LocationManager.NETWORK_PROVIDER) return false isProviderEnabled(LocationManager.GPS_PROVIDER) always returns false in android 2.3 GPS isProviderEnabled always return false LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) is not reliable, why?

  • ok thanks it closes when the user press the button but if the gps and network are enabled it still popups when i lunch the app...Any ideas on that? – Alex Jul 27 '18 at 06:49
  • i can't find my answer anywhere :/ thanks for your time my friend! – Alex Jul 27 '18 at 07:57
  • If you read the links I mentioned you should realize that the if statement you use although it's logical it is not reliable. –  Jul 27 '18 at 08:01