使用Android 23+确定清单中是否存在权限 [英] Determine whether a permission is present in the manifest with Android 23+

查看:108
本文介绍了使用Android 23+确定清单中是否存在权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个需要检查运行时权限的库.我已经获得了运行时权限的良好运行,并且理解了没有问题的用例.

I am creating a library that needs to check runtime permissions. I have got runtime permissions working fine and understand the use cases without issues.

但是,我想确认使用我们库的开发人员已将权限添加到他们的清单中.

However I would like to confirm that the developer using our library has added the permission to their manifest.

该库是一个基于位置的库,开发人员可以在清单中输入ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION,我需要能够确定它们在运行时使用了哪个(或两个).

The library is a location based library and the developer can either enter ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION into the manifest and I need to be able to determine which they have used (or both) at runtime.

我虽然使用程序包管理器来检查权限,但仍然可以正常工作:

I though using the package manager to check permission would work however this always seems to fail:

PackageManager pm = getPackageManager();
int granted = pm.checkPermission(
                      Manifest.permission.ACCESS_COARSE_LOCATION, 
                      getPackageName() );
if (granted == PackageManager.PERMISSION_GRANTED)
{
    // Use coarse for runtime requests
}
// granted is always PackageManager.PERMISSION_DENIED 

在Android v23 +中还有其他方法吗?

Is there some other way to do this in Android v23+?

推荐答案

立即关闭,通过 requestedPermissions数组 PackageInfo获取所有<uses-permission>要求的权限.

Off the cuff, retrieve the PackageInfo via PackageManager and getPackageInfo(getPackageName(), PackageManager.GET_PERMISSIONS). Then, look at the requestedPermissions array in the PackageInfo for all the <uses-permission>-requested permissions.

这篇关于使用Android 23+确定清单中是否存在权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆