Programmatically retrieve permissions from manifest.xml in android

回眸只為那壹抹淺笑 提交于 2019-12-18 02:12:32

问题


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

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.

Thank you.


回答1:


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;
}



回答2:


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.




回答3:


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()));



回答4:


I have a simple C# code, "using 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);
    }
}



回答5:


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);


来源:https://stackoverflow.com/questions/18236801/programmatically-retrieve-permissions-from-manifest-xml-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!