Android 模拟器未授予任何权限

No permissions granted on Android emulator

我写了一个简单的 Hello World 应用程序来调试我遇到的一些权限问题。

问题是我的应用无法获得使用任何东西的权限。是的,我正在使用

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

是的,我将该标签放在 AndroidManifest.xml

中的 <manifest> 下方

如果我从清单中删除所有 <uses-permission> 标签并转到 Settings > Apps > TestApp,它会显示 未请求权限

如果我把标签放回去,它会说 没有授予权限

如果我在实际设备上调试,它说权限在那里,所以看起来这个问题只发生在模拟器上!但是,当我 运行 来自 developer.android.com 的 android-play-location/LocationUpdates 项目使用相同的模拟器时它也可以工作,所以我的项目设置可能有问题。

有什么想法吗?

If I put the tags back in, it says No permissions granted

那是因为您在 Android 6.0 上进行测试,而您的 targetSdkVersion 设置为 23。

If I debug on an actual device, it says the permissions are there so it looks like this problem is only occurring on the emulator!

不,它只发生在 Android 6.0+。

However, it also works when I run the android-play-location/LocationUpdates project from developer.android.com using the same emulator so maybe there's something wrong with my project setup.

该项目的 targetSdkVersion 可能低于 23。

ACCESS_FINE_LOCATIONthe Android 6.0 runtime permission system 管辖。要么更新您的应用以使用该系统,要么将您的 targetSdkVersion 降低到 23 以下,直到您准备好这样做。

如果您 运行 在 M

上,只需请求位置许可
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
        requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION);
}else{
    //Do things as usual
}

等待结果

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] result){
    super.onRequestPermissionsResult(requestCode, permissions, result);


    if(requestCode == LOCATION_PERMISSION && result[0] == PackageManager.PERMISSION_GRANTED){
        //do things as usual init map or something else when location permission is granted
    }
}