Android notes custom broadcast notification bar

Source: Internet
Author: User


[Java]View plaincopy
  1. Notification n = new Notification (android. R. drawable. ic_menu_share, null, System. currentTimeMillis ());

Parameter: icon ID, text instantly sent to the status bar, current time

[Java]View plaincopy
  1. PendingIntent contentIntent = PendingIntent. getBroadcast (this, 0, intent, PendingIntent. FLAG_UPDATE_CURRENT );
  2. N. setLatestEventInfo (this, "good morning! "," Today is a clear day! ", ContentIntent );

3. Send to notification bar

[Java]View plaincopy
  1. Icationicationmanager mNM = (NotificationManager) getSystemService (Context. NOTIFICATION_SERVICE );
  2. MNM. notify (1001, n );

This completes the display of a notification, which is very simple!

[Java]View plaincopy
  1. Public void setLatestEventInfo (Context context,
  2. CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent ){
  3. // TODO: rewrite this to use Builder
  4. RemoteViews contentView = new RemoteViews (context. getPackageName (),
  5. R. layout. icationication_template_base );
  6. If (this. icon! = 0 ){
  7. ContentView. setImageViewResource (R. id. icon, this. icon );
  8. }
  9. If (priority <PRIORITY_LOW ){
  10. ContentView. setInt (R. id. icon,
  11. "SetBackgroundResource", R. drawable. icationication_template_icon_low_bg );
  12. ContentView. setInt (R. id. status_bar_latest_event_content,
  13. "SetBackgroundResource", R. drawable. icationication_bg_low );
  14. }
  15. If (contentTitle! = Null ){
  16. ContentView. setTextViewText (R. id. title, contentTitle );
  17. }
  18. If (contentText! = Null ){
  19. ContentView. setTextViewText (R. id. text, contentText );
  20. }
  21. If (this. when! = 0 ){
  22. ContentView. setViewVisibility (R. id. time, View. VISIBLE );
  23. ContentView. setLong (R. id. time, "setTime", when );
  24. }
  25. If (this. number! = 0 ){
  26. NumberFormat f = NumberFormat. getIntegerInstance ();
  27. ContentView. setTextViewText (R.id.info, f. format (this. number ));
  28. }
  29. This. contentView = contentView;
  30. This. contentIntent = contentIntent;
  31. }
We can see that it actually creates a RemoteViews for us using the default layout of the system,

[Java]View plaincopy
  1. ContentView. setImageViewResource (R. id. icon, this. icon );
It is actually setting the icon:

[Java]View plaincopy
  1. RemoteViews. setImageViewBitmap (int viewId, Bitmap bitmap)

[Java]View plaincopy
  1. Class <?> Clazz = Class. forName ("com. android. internal. R$ id ");
  2. Field field = clazz. getField ("icon ");
  3. Field. setAccessible (true );
  4. Id_icon = field. getInt (null );

[Java]View plaincopy
  1. N. setLatestEventInfo (context, title, msg, contentIntent );
  2. N. flags | = Notification. FLAG_AUTO_CANCEL;
  3. If (n. contentView! = Null & icon! = Null ){
  4. N. contentView. setImageViewBitmap (InternalID. id_icon, icon );
  5. }
Send a notification. Drop down the notification bar to see if the icon has changed. ^_^



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.