GPS is on, but the code is still not getting executed. Please see details

73 Views Asked by At

I'm developing an app in which I want to execute a piece of code only if GPS is enabled.

I'm checking the GPS using this piece of code:

isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

And now, I'm using it like this:

if (isGPSEnabled) {
            gps_off.setVisibility(View.INVISIBLE);
            ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
            locationProvider.getLastKnownLocation()
                    .subscribe(new Action1<Location>() {
                        @Override
                        public void call(Location location) {
                            currentLatDoubleA = location.getLatitude();
                            currentLngDoubleA = location.getLongitude();
                            Toast.makeText(getBaseContext(), "User's location retrieved", Toast.LENGTH_SHORT).show();
                        }
                    });
//        }
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    retrieveHRequest();
                }
            }, 2000);
        } else {
            gps_off.setVisibility(View.VISIBLE);
            Snackbar snackbar = Snackbar
                    .make(coordinatorLayout, "No location detected!", Snackbar.LENGTH_LONG)
                    .setAction("RETRY", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

                            ifLocationAvailable();
                        }
                    });
            snackbar.setDuration(Snackbar.LENGTH_INDEFINITE);
            snackbar.show();
        }

The problem is that even after turning the GPS on, the Snackbar is still popping up showing No location detected! message which means the code which is supposed to execute when GPS is off is getting executed even when GPS in on.

Another thing to note is that when I'm reopening the app after closing it, the code which should be executed when GPS is on is getting executed without any problem.

What's going wrong here?

Please let me know.

1

There are 1 best solutions below

6
On

In your application you want to implement broadcast receiver for observe changing the state of GPS. Then only the application understand about your changing status of GPS.

This code may helps you

 public class BootReceiver extends BroadcastReceiver  {

       @Override
    public void onReceive(Context context, Intent intent) {
        LocationManager locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            // gps enabled and do your action here        } else {
            // gps disabled and do you snack bar action
        }
    }
}