Android interface NotificationManager USES Bitmap as the icon

  • 2020-05-07 20:24:27
  • OfStack

Today, I saw the question "[Android interface]NotificationManager how to use Bitmap as an icon" in EOE q&a, and there is no good case in the forum search

Feature 1 simple demo for your reference
Today's release is NotificationManager using Bitmap as the icon
key code
 
public void notification(int flag) 
{ 
Notification notification = new Notification(); 
// Set up the statusbar According to the icon 
notification.icon = R.drawable.icon; 
// Set up the statusbar Text message displayed  
// myNoti.tickerText= new_msg ; 
notification.flags = Notification.FLAG_AUTO_CANCEL; 
// Set up the notification Occurs with a default sound  
notification.defaults = Notification.DEFAULT_SOUND; 
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); 
Bitmap bitmap=null; 
if(flag==0) 
{ 
bitmap=drawableToBitmap(this.getResources().getDrawable(R.drawable.icon)); 
}else 
{ 
// This is the key place, either from the web or sdcard Get the picture on and convert to bitmap You can  
bitmap=drawableToBitmap(this.getResources().getDrawable(R.drawable.alert_dialog_icon)); 
} 
contentView.setImageViewBitmap(R.id.notification_icon, bitmap); 
contentView.setTextViewText(R.id.app_name, "Custom notification"); 
notification.contentView = contentView; 
Intent intent = new Intent(this, MainActivity.class); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 
PendingIntent.FLAG_UPDATE_CURRENT); 
notification.contentIntent = contentIntent; 
// According to Notification 
Random random = new Random(new Date().getTime()); 
mNotificationManager.notify(random.nextInt(1000000), notification); 
} 
// conversion drawableToBitmap 
public static Bitmap drawableToBitmap(Drawable drawable) 
{ 
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); 
Canvas canvas = new Canvas(bitmap); 
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 
drawable.draw(canvas); 
return bitmap; 
} 

Source download: NotificationIcon.rar

Related articles: