-2

My application needs GPS, so when a specific operation is performed I check if the GPS is enabled as following (and as suggested by Marcus in this other question):

if (!locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER )) {
    buildAlertMessageNoGps();
}

rootView = inflater.inflate(R.layout.fragment_alert, container, false);
[...]

The problem is that the user, from the location settings, could not enable the GPS (e.g., by clicking on the "back" button and going back to the application). In this particular case, the fragment injects the layout for the corresponding process, but the GPS is still disabled. How to solve that?

Community
  • 1
  • 1
redcrow
  • 1,743
  • 3
  • 25
  • 45

2 Answers2

1

I guess this is the method that you are looking for. You have to check for android version above KitKat.

@SuppressLint("InlinedApi") @SuppressWarnings("deprecation")
public static boolean isLocationEnabled(Context context) {
    int locationMode = 0;
    String locationProviders;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
        try {
            locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);

        } catch (SettingNotFoundException e) {
            e.printStackTrace();
        }

        return locationMode != Settings.Secure.LOCATION_MODE_OFF;

    }else{
        locationProviders =     Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        return !TextUtils.isEmpty(locationProviders);
    }
} 
loshkin
  • 1,600
  • 2
  • 21
  • 38
  • So, do you suggest to replace my ``if`` condition with your method? But, I still don't understand how I can be sure that user, after entering in the location settings, really enables the GPS option. – redcrow Jul 02 '15 at 09:11
  • Yes, I suggest that you replace it with this method and it will return the "The degree of location access enabled by the user." according the the javadoc provided for the flags. – loshkin Jul 02 '15 at 09:22
  • Sorry, but I don't see a solution to my problem. Suppose I'm the user and the GPS is currently disabled. I click on the menu item, a dialog ask me if I want to enable the GPS, so I click on "Yes". Now, in the location settings, instead of enabling the GPS, I click on the "Back" button, so I go back to the application and the GPS is still disabled, while the layout is now injected. How does your method solve this case? Sorry, but I still don't understand... – redcrow Jul 02 '15 at 09:30
  • This check will give you the current level of location access enabled by the user. If the user presses back it will return false next time it is triggered. So you have to make this check again after the user comes back from the settings. – loshkin Jul 02 '15 at 09:38
  • Yes, but the problem is just that: the check after the user comes back from the settings. The two operations are asynchronous, so I cannot stop the injection of the layout... Where should I check again? – redcrow Jul 02 '15 at 09:43
0

I solved by replacing startActivity(...) with startActivityForResult(...) in the buildAlertMessageNoGps() method. In doing so, I can check again if the GPS has been enabled in the callback function onActivityResult(...).

redcrow
  • 1,743
  • 3
  • 25
  • 45