Programmatically retrieve permissions from manifest.xml in android

前端 未结 5 706
无人共我
无人共我 2020-12-05 18:39

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 ent

相关标签:
5条回答
  • 2020-12-05 19:01

    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);
    
    0 讨论(0)
  • 2020-12-05 19:05

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-05 19:06

    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()));
    
    0 讨论(0)
  • 2020-12-05 19:07

    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.

    0 讨论(0)
  • 2020-12-05 19:14

    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;
    }
    
    0 讨论(0)
提交回复
热议问题