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
Related videos on Youtube
Author by
Questioner
Updated on June 04, 2022Comments
-
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 almost 8 yearsAre you using Android 6.0 or API 23?
-
Sunil Sunny almost 8 yearsYou are getting no errors ?
-
Questioner almost 8 yearsI'm using Android 6.0 @SohailZahid The error is that I can't get this permission.
-
Selvin almost 8 yearsObvioulsy you will not get permission until
onRequestPermissionsResult
checking permissions right afterActivityCompat.requestPermissions
doesn't make sens
-
-
Questioner almost 8 yearsI changed that line to what you wrote and hasLocationPermission still returns 0
-
Questioner almost 8 yearsI just did this and it did nothing.
-
Selvin almost 8 yearsUse 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 almost 8 yearsI'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 almost 8 yearsAnd you are aware, that once you installed an application and granted the permission, it is later always true?
-
Questioner almost 8 yearsI'm aware that that is supposed to be the case.
-
R. Zagórski almost 8 yearsSo maybe the problem is with fetching location and not permissions.
-
Questioner almost 8 yearsYes.
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
always returns null -
R. Zagórski almost 8 yearsDo you have GPS turned on? Is the position fixed? Checked on other map applications?
-
Questioner almost 8 yearsLet us continue this discussion in chat.