<permission>
和 <permission-group>
的 XML 规范指的是您在“设置”| 中看到的图标。应用信息 |权限。
如果我已经通过查询 PackageInfo 获得了应用程序所需的权限(例如“android.permission.INTERNET”),那么如何获取关联的权限和权限组图标?
这是我的代码。
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;
}
}
据此,以下方法应该有效: 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
。
我遇到了同样的问题,图标始终为零。 这件事最终是如何解决的呢? 我知道这是一个旧线程。