Issue
I am making an app that displays the details of the Android device's installed permissions. Currently, I am displaying an AlertDialog with the icon for said permission but I would also like to deliver a notification as per the user's request. My code is:
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;
}
However, the v4 NotificationCompat.Builder can not take a Drawable only an int, so what should I do?
Solution
You can get the icon resource id like that (assuming that permission is the String that you also use in your question):
final PackageManager pm = context.getPackageManager();
PermissionInfo info = pm.getPermissionInfo(permission, PackageManager.GET_META_DATA);
int iconResourceId = info.icon;
And then...
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(iconResourceId);
Answered By - Markus Penguin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.