我正在创建一个应用程序,显示Android设备的已安装权限的详细信息。目前,我正在显示带有所述权限图标的AlertDialog,但我还想根据用户的请求发送通知。我的代码是:
Drawable icon;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String permission = getArguments().getString("permission");
String label = "";
String description = "";
icon = null;
try {
label = getActivity().getPackageManager().getPermissionInfo(permission, PackageManager.GET_META_DATA).loadLabel(getActivity().getPackageManager()).toString();
description = getActivity().getPackageManager().getPermissionInfo(permission, PackageManager.GET_META_DATA).loadDescription(getActivity().getPackageManager()).toString();
icon = getActivity().getPackageManager().getPermissionInfo(permission, PackageManager.GET_META_DATA).loadIcon(getActivity().getPackageManager());
} catch (Exception ignored) {}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("Permission")
.setMessage("Permission: " + getArguments().getString("permission") + Character.toString((char) 10) + "Label: " + label + Character.toString((char) 10) + "Description: " + description)
.setPositiveButton("Dismiss", null)
.setCancelable(false);
if (icon != null) {
builder.setIcon(icon);
}
final AlertDialog alertDialog = builder.create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
// Show notification here
}
});
return alertDialog;
}
但是,v4 NotificationCompat.Builder不能将Drawable仅作为int,所以我该怎么办?
答案 0 :(得分:0)
您可以像这样获取图标资源ID(假设permission
是您在问题中也使用的字符串):
final PackageManager pm = context.getPackageManager();
PermissionInfo info = pm.getPermissionInfo(permission, PackageManager.GET_META_DATA);
int iconResourceId = info.icon;
然后......
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(iconResourceId);
答案 1 :(得分:0)
R.drawable是一个整数,因此是R.color或R.string或R.layout