4

I have the following in my manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

I also have the location permission set in my phone's settings.

Yes, GPS is enabled on the phone.

Here, I check for the permission:

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            int hasLocationPermission = mContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
            Log.d(TAG, "location permission: " + hasLocationPermission); // 0

            if (hasLocationPermission != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            }
            hasLocationPermission = mContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
            Log.d(TAG, "location permission: " + hasLocationPermission); // still 0
        }

If I have the permission in my manifest and in my phone's settings for the app, why is it still preventing me from accessing the location? It is returning 0.0 for both latitude and longitude because of this.

Questioner
  • 2,451
  • 4
  • 29
  • 50

6 Answers6

1

Declare Where you use Oncreate Or Others

    if (Build.VERSION.SDK_INT >= 23){


    if (checkPermission(Manifest.permission.ACCESS_FINE_LOCATION,getApplicationContext(),this)) {
    //You fetch the Location here

    //code to use the 
    }
    else
    {
    requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,PERMISSION_REQUEST_CODE_LOCATION,getApplicationContext(),this);
    }

    }

Also Declare In class

    public static void requestPermission(String strPermission,int perCode,Context _c,Activity _a){

    if (ActivityCompat.shouldShowRequestPermissionRationale(_a,strPermission)){
        Toast.makeText(getApplicationContext(),"GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
        } else {

        ActivityCompat.requestPermissions(_a,new String[]{strPermission},perCode);
        }
    }

public static boolean checkPermission(String strPermission,Context _c,Activity _a){
    int result = ContextCompat.checkSelfPermission(_c, strPermission);
    if (result == PackageManager.PERMISSION_GRANTED){

        return true;

    } else {

        return false;

    }
    }

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {

    case PERMISSION_REQUEST_CODE_LOCATION:
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        fetchLocationData();

        } else {

        Toast.makeText(getApplicationContext(),"Permission Denied, You cannot access location data.",Toast.LENGTH_LONG).show();

        }
        break;

    }
}

For other lower device you should give the permission as you declare

Arjun saini
  • 4,223
  • 3
  • 23
  • 51
0

add this permission and check once

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Manohar
  • 22,116
  • 9
  • 108
  • 144
0

For Android 6.0 we have to get runtime permission for user only declaraing permission in manifest will not enough.

Check my this post.

Community
  • 1
  • 1
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
  • I'm getting runtime permission already. If the permission isn't granted the user gets a pop up and grants the permission (apparently but not really). I wrote in my question that the permission was granted in Android settings for the app. – Questioner Jul 01 '16 at 09:56
0

You are mixing methods of checking permissions.

One time, you call:

int hasLocationPermission = mContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);

the next time:

ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

Make sure the first line is:

int hasLocationPermission = ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION);

And your activity extends from AppCompatActivity.

R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
0
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Narendra Sorathiya
  • 3,770
  • 2
  • 34
  • 37
-2

Use the non-static requestPermissions in Activity, don't use the static one in ActivityCompat. ActivityCompat.requestPermissions also not work in my previous project, and I don't know why.

I have found a good way to check and request permissions. See my answer in this post.

Community
  • 1
  • 1
Jeffrey Chen
  • 1,777
  • 1
  • 18
  • 29
  • *Use the non-static requestPermissions in Activity* ... it does't exist on API < 23 ... *don't use the static one in ActivityCompat* [why? it is just the same code as in your link](https://github.com/android/platform_frameworks_support/blob/master/v4/java/android/support/v4/app/ActivityCompat.java#L313) ... – Selvin Jul 01 '16 at 09:56