Android icationicationmanager uses Bitmap as an icon

Source: Internet
Author: User

Today, we can see the question "How icationicationmanager uses Bitmap for icons on the [Android interface]" in the eoe q & A. There are no good cases for searching in the Forum.

Close up a simple demo for your reference
Today, icationicationmanager uses Bitmap as an icon.
Key code Copy codeThe Code is as follows: public void notification (int flag)
{
Notification notification = new Notification ();
// Set the icon displayed in statusbar
Notification. icon = R. drawable. icon;
// Set the text displayed by statusbar
// MyNoti. tickerText = new_msg;
Notification. flags = Notification. FLAG_AUTO_CANCEL;
// Set the default sound when notification occurs
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. You can obtain images from the network or sdcard and convert them to bitmap.
Bitmap = drawableToBitmap (this. getResources (). getDrawable (R. drawable. alert_dialog_icon ));
}
ContentView. setImageViewBitmap (R. id. icationication_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;
// Display Notification
Random random = new Random (new Date (). getTime ());
MNotificationManager. Y (random. nextInt (1000000), notification );
}
// Convert 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;
}

Download the source code: icationicationicon.rar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.