Java 在android中以编程方式从manifest.xml中检索权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18236801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:37:36  来源:igfitidea点击:

Programmatically retrieve permissions from manifest.xml in android

javaandroidpermissionsandroid-manifest

提问by Alexis Le Compte

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

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

I read the post herebut 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.

在这里阅读了这篇文章,但我对答案并不完全满意。我想在 android API 中应该有一个类,它允许从清单中检索信息。

Thank you.

谢谢你。

采纳答案by Phil

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

您可以使用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;
}

回答by crm27

You can used this function:

您可以使用此功能:

public boolean hasPermission(Context ctx,String permission){
        return ctx.getPackageManager().checkPermission(permission, ctx.getPackageName())
               == PackageManager.PERMISSION_GRANTED); 
}

Usage:

用法:

hasPermission(this,Manifest.permission.GET_ACCOUNTS);

回答by Yousha Aleayoub

Use this:

用这个:

public static String getListOfPermissions(final Context context)
{
    String _permissions = "";

    try
    {
        final AssetManager _am = context.createPackageContext(context.getPackageName(), 0).getAssets();
        final XmlResourceParser _xmlParser = _am.openXmlResourceParser(0, "AndroidManifest.xml");
        int _eventType = _xmlParser.getEventType();
        while (_eventType != XmlPullParser.END_DOCUMENT)
        {
            if ((_eventType == XmlPullParser.START_TAG) && "uses-permission".equals(_xmlParser.getName()))
            {
                for (byte i = 0; i < _xmlParser.getAttributeCount(); i ++)
                {
                    if (_xmlParser.getAttributeName(i).equals("name"))
                    {
                        _permissions += _xmlParser.getAttributeValue(i) + "\n";
                    }
                }
            }
            _eventType = _xmlParser.nextToken();
        }
        _xmlParser.close(); // Pervents memory leak.
    }
    catch (final XmlPullParserException exception)
    {
        exception.printStackTrace();
    }
    catch (final PackageManager.NameNotFoundException exception)
    {
        exception.printStackTrace();
    }
    catch (final IOException exception)
    {
        exception.printStackTrace();
    }

    return _permissions;
}
// Test: Log.wtf("test", getListOfPermissions(getApplicationContext()));

回答by Anis

Here's a useful utility method that does just that.

这是一个有用的实用方法,可以做到这一点。

public static String[] retrievePermissions(Context context) {
    try {
        return context
                .getPackageManager()
                .getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS)
                .requestedPermissions;
    } catch (PackageManager.NameNotFoundException e) {
        throw new RuntimeException ("This should have never happened.", e);
    }
}

You can get a working class from this gist.

你可以从这个 gist 获得一个工人阶级

回答by Anis

I have a simple C# code, "using System.Xml"

我有一个简单的 C# 代码,“使用 System.Xml”

private void ShowPermissions()
   {
    XmlDocument doc = new XmlDocument();
    doc.Load("c:\manifest.xml");

    XmlNodeList nodeList = doc.GetElementsByTagName("uses-permission");


    foreach(XmlNode node in nodeList)
    {
        XmlAttributeCollection Attr = node.Attributes;
        string Permission=Attr["android:permission"].Value;

        MessageBox.Show(Permission);
    }
}