我正在处理应用程序,在该应用程序中,我正在获取其他应用程序信息。并在通知构建器上显示信息。 我正在获取其他应用程序图标,并希望在通知上显示。但获取信息包含可绘制的图像。
那么,如何在通知上显示可绘制的图像。 这是我的代码示例。
String packageName=appSetting.getHeavyDrainingAppName();
ApplicationInfo app = getPackageManager().getApplicationInfo(packageName, PackageManager.GET_META_DATA);
int iconId = ......????
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(iconId)
.setContentTitle(getResources().getString(R.string.heavy_drain_text))
.setContentText("Tap to stop")
.setOngoing(true);
答案 0 :(得分:3)
There is some points about this question:
1st :
You can set the LargeIcon from a Drawable (instead of Resource id), like the following
Drawable icon = getActivity().getPackageManager().getApplicationIcon(packageName);
Bitmap bitmap = ((BitmapDrawable)icon).getBitmap();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setLargeIcon(bitmap)
.setContentTitle("hahah")
.setContentText("Tap to stop")
.setOngoing(true);
2nd :
If you need to set a SmallIcon in API below 23, you will need to set a resource id like R.drawable.your_resource
.
The NotificationCompat.Builder does not allow you to use Drawables or Bitmaps in
setSmallIcon()`.
3rd :
fortunately , the support has been expanded to Icon
type on setSmallIcon()
in version 23+, using the Notification.Builder, like following :
Drawable icon = getActivity().getPackageManager().getApplicationIcon(packageName);
Bitmap bitmap = ((BitmapDrawable)icon).getBitmap();
Notification.Builder mBuilder =
new Notification.Builder(context)
.setSmallIcon(Icon.createWithBitmap(bitmap))
.setLargeIcon(bitmap)
.setContentTitle("hahah")
.setContentText("Tap to stop")
.setOngoing(true);