[Java]View plaincopy
- 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
- PendingIntent contentIntent = PendingIntent. getBroadcast (this, 0, intent, PendingIntent. FLAG_UPDATE_CURRENT );
- N. setLatestEventInfo (this, "good morning! "," Today is a clear day! ", ContentIntent );
3. Send to notification bar
[Java]View plaincopy
- Icationicationmanager mNM = (NotificationManager) getSystemService (Context. NOTIFICATION_SERVICE );
- MNM. notify (1001, n );
This completes the display of a notification, which is very simple!
[Java]View plaincopy
- Public void setLatestEventInfo (Context context,
- CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent ){
- // TODO: rewrite this to use Builder
- RemoteViews contentView = new RemoteViews (context. getPackageName (),
- R. layout. icationication_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. icationication_template_icon_low_bg );
- ContentView. setInt (R. id. status_bar_latest_event_content,
- "SetBackgroundResource", R. drawable. icationication_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;
- }
We can see that it actually creates a RemoteViews for us using the default layout of the system,
[Java]View plaincopy
- ContentView. setImageViewResource (R. id. icon, this. icon );
It is actually setting the icon:
[Java]View plaincopy
- RemoteViews. setImageViewBitmap (int viewId, Bitmap bitmap)
[Java]View plaincopy
- Class <?> Clazz = Class. forName ("com. android. internal. R$ id ");
- Field field = clazz. getField ("icon ");
- Field. setAccessible (true );
- Id_icon = field. getInt (null );
[Java]View plaincopy
- N. setLatestEventInfo (context, title, msg, contentIntent );
- N. flags | = Notification. FLAG_AUTO_CANCEL;
- If (n. contentView! = Null & icon! = Null ){
- N. contentView. setImageViewBitmap (InternalID. id_icon, icon );
- }
Send a notification. Drop down the notification bar to see if the icon has changed. ^_^