Android does not implement custom notification bar icons using custom layouts _android

Source: Internet
Author: User
Tags current time

This example describes how Android implements custom notification bar icons without using custom layouts. Share to everyone for your reference, specific as follows:

Customize the notification bar icon? It's not that simple. Custom layouts are a cinch!

Yes, there are XML layout files Of course everything is very simple, if you do not give you layout files to use?

Listen to me slowly!

How do you create a notification first?
1.new A

Copy Code code as follows:
Notification n = new Notification (Android. R.drawable.ic_menu_share, NULL, System.currenttimemillis ());

Parameters: Icon ID, text sent to the status bar instant, current time

2. Set details: Title, content, intent

Pendingintent contentintent = pendingintent.getbroadcast (this, 0, intent, pendingintent.flag_update_current);
N.setlatesteventinfo (This, "Good morning!") "It's a fine day," said the man. ", contentintent);

3. Send to notification bar

Notificationmanager MNM = (notificationmanager) getsystemservice (context.notification_service);
Mnm.notify (1001, N);

This completes a notice of the display, very simple!

Let's see what N.setlatesteventinfo did.

public void Setlatesteventinfo (context context, charsequence Contenttitle, charsequence contenttext, pendingintent con 
   Tentintent) {//Todo:rewrite this to use Builder remoteviews Contentview = new Remoteviews (Context.getpackagename (),
 R.layout.notification_template_base);
 if (This.icon!= 0) {Contentview.setimageviewresource (R.id.icon, This.icon); } if (priority < Priority_low) {Contentview.setint (R.id.icon, "Setbackgroundresource", r.drawable.notification
  _TEMPLATE_ICON_LOW_BG); Contentview.setint (r.id.status_bar_latest_event_content, "Setbackgroundresource", R.drawable.notification_bg_low
 );
 } if (Contenttitle!= null) {Contentview.settextviewtext (r.id.title, contenttitle);
 } if (ContentText!= null) {Contentview.settextviewtext (R.id.text, ContentText);
  } if (this.when!= 0) {contentview.setviewvisibility (r.id.time, view.visible);
 Contentview.setlong (R.id.time, "settime", when); } if (this.number!= 0) {NumberFormat f = NumberfoRmat.getintegerinstance ();
 Contentview.settextviewtext (R.id.info, F.format (This.number));
 } This.contentview = Contentview;
This.contentintent = contentintent;

 }

As you can see, he's actually using the system default layout to create a remoteviews for us, Remoteviews is a view thatis designed to be displayed across processes , for details reference to official documentation: http:// Developer.android.com/intl/zh-cn/reference/android/widget/remoteviews.html

Look at this sentence:

Copy Code code as follows:
Contentview.setimageviewresource (R.id.icon, This.icon);

is actually set the icon:

Parameter 1: ID of the ImageView used to display the icon
Parameter 2: Icon ID

But there is another way to do this:

Copy Code code as follows:
Remoteviews.setimageviewbitmap (int viewid, Bitmap Bitmap)

Use Bitmap to set the icon.

And Notifycation has a parameter: Notification.contentview, look carefully, setlastesteventinfo method created in Remoteviews is him, so you know what to do!

But here's another question? R.id.icon How to get, this thing actually in COM.ANDROID.INTERNAL.R this inside, but this class we can not access to do?

Reflection Bai, Java reflection is omnipotent ah, anything can be taken as long as he is.

Class<?> clazz = Class.forName ("Com.android.internal.r$id"); 
 Field field = Clazz.getfield ("icon"); 
 Field.setaccessible (true); 
 int id_icon = Field.getint (null); 

N.setlatesteventinfo (context, title, MSG, contentintent);
N.flags |= Notification.flag_auto_cancel;
if (N.contentview!= null && icon!= null) {
 n.contentview.setimageviewbitmap (Id_icon, icon);
}

Send a notice, drop the notice bar to see, the icon is not changed ^_^

In addition here is a small detail, is that you new Notifycation () is the icon will be used as a status bar of small icons, small icon size in the hdpi below the 32x32 can be

So you can pass the small icon for the first time and then set the big icon through the Contentview, so that's OK.

I hope this article will help you with the Android program.

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.