本文介绍了在android中以编程方式从manifest.xml中检索权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须以编程方式从 android 应用程序的 manifest.xml 中检索权限,但我不知道该怎么做.

I have to programmatically retrieve permissions from the manifest.xml of an android application and I don't know how to do it.

我阅读了此处的帖子 但我对答案并不完全满意.我想 android API 中应该有一个允许从清单中检索信息的类.

I read the post here but I am not entirely satisfied by the answers.I guess there should be a class in the android API which would allow to retrieve information from the manifest.

谢谢.

推荐答案

您可以使用 PackageManager 获取应用程序请求的权限(可能未授予):

You can get an application's requested permissions (they may not be granted) using PackageManager:

PackageInfo info = getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
String[] permissions = info.requestedPermissions;//This array contains the requested permissions.

我在一个实用方法中使用它来检查是否声明了预期的权限:

I have used this in a utility method to check if the expected permission is declared:

//for example, permission can be "android.permission.WRITE_EXTERNAL_STORAGE"
public boolean hasPermission(String permission)
{
    try {
        PackageInfo info = getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
        if (info.requestedPermissions != null) {
            for (String p : info.requestedPermissions) {
                if (p.equals(permission)) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

这篇关于在android中以编程方式从manifest.xml中检索权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:53