Can't get location permission for Android app

12,825

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

Share:
12,825

Related videos on Youtube

Questioner
Author by

Questioner

Updated on June 04, 2022

Comments

  • Questioner
    Questioner almost 2 years

    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.

    • Sohail Zahid
      Sohail Zahid almost 8 years
      Are you using Android 6.0 or API 23?
    • Sunil Sunny
      Sunil Sunny almost 8 years
      You are getting no errors ?
    • Questioner
      Questioner almost 8 years
      I'm using Android 6.0 @SohailZahid The error is that I can't get this permission.
    • Selvin
      Selvin almost 8 years
      Obvioulsy you will not get permission until onRequestPermissionsResult checking permissions right after ActivityCompat.requestPermissions doesn't make sens
  • Questioner
    Questioner almost 8 years
    I changed that line to what you wrote and hasLocationPermission still returns 0
  • Questioner
    Questioner almost 8 years
    I just did this and it did nothing.
  • Selvin
    Selvin almost 8 years
    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 ...
  • Questioner
    Questioner almost 8 years
    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.
  • R. Zagórski
    R. Zagórski almost 8 years
    And you are aware, that once you installed an application and granted the permission, it is later always true?
  • Questioner
    Questioner almost 8 years
    I'm aware that that is supposed to be the case.
  • R. Zagórski
    R. Zagórski almost 8 years
    So maybe the problem is with fetching location and not permissions.
  • Questioner
    Questioner almost 8 years
    Yes. locationManager.getLastKnownLocation(LocationManager.GPS_PRO‌​VIDER) always returns null
  • R. Zagórski
    R. Zagórski almost 8 years
    Do you have GPS turned on? Is the position fixed? Checked on other map applications?
  • Questioner
    Questioner almost 8 years