如何获取Android权限的图标?

时间:2015-08-25 13:55:23

标签: android permissions

<permission><permission-group>的XML规范是指您在“设置”中看到的图标应用信息|权限。

如果我已经拥有了查询PackageInfo的字符串中的应用程序所需的权限,例如“android.permission.INTERNET”,我该如何获取关联的权限和权限组图标?

2 个答案:

答案 0 :(得分:3)

这是我的代码。

package sample.com.sample_android;

import android.Manifest;
import android.content.pm.PackageManager;
import android.content.pm.PermissionGroupInfo;
import android.content.pm.PermissionInfo;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String ANDROID = "android";
    private PackageManager mPackageManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPackageManager = getPackageManager();

        String permission = Manifest.permission.INTERNET;

        Drawable drawable = getPermissionDrawable(permission);
        ImageView imageView  = (ImageView) findViewById(R.id.image);
        imageView.setImageDrawable(drawable);

        TextView textView = (TextView) findViewById(R.id.text);
        textView.setText(permission);
    }

    @Nullable
    private Drawable getPermissionDrawable(String permission) {
        Drawable drawable = null;
        try {
            PermissionInfo permissionInfo = mPackageManager.getPermissionInfo(permission, 0);
            PermissionGroupInfo groupInfo = mPackageManager.getPermissionGroupInfo(permissionInfo.group, 0);
            drawable = mPackageManager.getResourcesForApplication(ANDROID).getDrawable(groupInfo.icon);
        } catch (PackageManager.NameNotFoundException|Resources.NotFoundException e) {
        }
        return drawable;
    }
}

enter image description here

答案 1 :(得分:2)

据此,以下方法应该有效: http://developer.android.com/reference/android/content/pm/PackageManager.html

PackageManager pm = context.getPackageManager();
PermissionInfo info = pm.getPermissionInfo("PERMISSION_NAME", OPTIONAL_FLAGS);
int drawableResource = info.icon;

PackageManager中还有一个名为getPermissionGroupInfo的方法。