问题
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.
回答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
回答2:
add this permission and check once
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
回答3:
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.
回答4:
For Android 6.0 we have to get runtime permission for user only declaraing permission in manifest will not enough.
Check my this post.
回答5:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
回答6:
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.
来源:https://stackoverflow.com/questions/38142097/cant-get-location-permission-for-android-app