I'm developing an app in which GPS is required to detect user's current location and proceed further. So, I'm using this code: isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); to detect whether GPS is on or not and prompting user to turn it ON if it's OFF. This code is perfectly detecting if it's OFF, but when I'm turning the GPS ON and trying to proceed further. The above code is failing to detect that the GPS is ON and prompting user again to turn the GPS ON even when it's ON.

Here's how I'm trying to proceed further after turning ON the GPS:

public void btnRetryGpsEvents() {

        if (isGPSEnabled) {
            ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
            locationProvider.getLastKnownLocation()
                    .subscribe(new Action1<Location>() {
                        @Override
                        public void call(Location location) {
                            currentLatDouble = location.getLatitude();
                            currentLngDouble = location.getLongitude();

                        }
                    });

            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (currentLatDouble != null || currentLngDouble != null) {
                            retrieveHWrapper();
                            btnRetryGps.setVisibility(View.INVISIBLE);
                            gps_off.setVisibility(View.INVISIBLE);
                            Toast.makeText(getBaseContext(), "Loading h-requests...", Toast.LENGTH_LONG).show();
                        } else {
                            gps_off.setVisibility(View.VISIBLE);
                            progressBarLoadingRequests.setVisibility(View.INVISIBLE);
                            btnRetryGps.setVisibility(View.VISIBLE);
                            btnRetryGps.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    btnRetryGpsEvents();
                                }
                            });
                        }
                }
            }, 2000);
        } else {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

            // Setting Dialog Title
            alertDialog.setTitle("GPS settings");

            // Setting Dialog Message
            alertDialog.setMessage("GPS is not enabled. Enable it in settings menu.");

            // On pressing Settings button
            alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(intent);
                }
            });

            // on pressing cancel button
            alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            // Showing Alert Message
            alertDialog.show();

            gps_off.setVisibility(View.VISIBLE);
            progressBarLoadingRequests.setVisibility(View.INVISIBLE);
            btnRetryGps.setVisibility(View.VISIBLE);
            btnRetryGps.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    btnRetryGpsEvents();
                }
            });
        }
    }

I've already asked for permission in onCreate() method:

int hasWriteContactsPermission = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) & ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) & ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) & ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_SETTINGS);
        if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.WRITE_SETTINGS) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) && !ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) && !ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) && !ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_SETTINGS)) {
                    showMessageOKCancel("You need to allow access to few permissions so that the app can work as expected.",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_SETTINGS},
                                            REQUEST_RUNTIME_PERMISSION);
                                }
                            });
                    return;
                }
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_SETTINGS},
                        REQUEST_RUNTIME_PERMISSION);
                return;
            }
        }

So, the bottomline is that even after turning the GPS ON and clicking on 'RETRY' button, the code which should have get executed when GPS is ON is not getting executed!

What's going wrong here?

Please let me know.

1

There are 1 best solutions below

2
On BEST ANSWER

Maybe your boolean still returns false because you did not perform a recheck. You can do that by replacing the condition in your retry function isGPSEnabled, which I assume still returns true. Do it like:

public void btnRetryGpsEvents() {

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
        locationProvid//... REST OF YOUR CODE ...//

By the way, you should know that the isProviderEnabled flag will only return true some time after turning on the GPS and not immediately. It is because the device still has to have a connection to the GPS satellites or networks for it to indicate that the provider is enabled. So it is recommended to check and recheck service availability after turning on the device's GPS until you get a connection. In addition, I use this for initial checking:

LocationManager locationManager;
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
  // TODO Execute code if provider is not available. Also check for network connection
}