ContextCompat.checkSelfPermission无效的问题

最近发现一个问题,我在使用ContextCompat.CheckSelfPermission()时无论如何开关权限返回值都是PackageManager.PERMISSION_GRANTED,而使用PackageManager.checkPermission()的时候返回值又始终都是PackageManager.PERMISSION_DENIED;

经过多番尝试和查找资料发现原因:

If your application is targeting an API level before 23 (Android M) then both:ContextCompat.CheckSelfPermission and Context.checkSelfPermission doesn't work and always returns 0 (PERMISSION_GRANTED). Even if you run the application on Android 6.0 (API 23).

在targetSdkVersion小于23(Android M)的时候,ContextCompat.CheckSelfPermission 和Context.checkSelfPermission方法都不能正常工作并且始终返0(PERMISSION_GRANTED),即使你的应用运行在Android6.0(API 23)的设备上。

解决办法:

As I said in the 1st point, if you targeting an API level before 23 on Android 6.0 then ContextCompat.CheckSelfPermission and Context.checkSelfPermission doesn't work. Fortunately you can use PermissionChecker.checkSelfPermission to check run-time permissions.  

可以使用PermissionChecker.checkSelfPermission()去检查权限是否被授予。

你可能感兴趣的:(【Android,-,路上的那些事】)