androidpermissionslocation

checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) returns true even though my GPS is off


I'm trying to prompt the user to activate GPS if the permission is not granted. I'm not able to get to the request part because the check is always returning true even when my GPS is turned off.

Here's my code :

if (ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(), new String[]{ACCESS_FINE_LOCATION}, 1);
}

I'm expecting the condition to return true since my GPS is off, but instead it is returning false.

Can you please explain to me why i'm getting false for this ?

Thank you in advance for your help.


Solution

  • You are asking user to give your app location permission. To turn on the gps you need another code, so after getting the true value do something like this:

    Intent gpsOptionsIntent = new Intent(  
    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
    startActivity(gpsOptionsIntent);