Today summed up the use of notification, share with you.
Mainactivity.java:
This article references: http://www.jb51.net/article/36567.htm,http://www.cnblogs.com/linjiqin/archive/2011/12/14/2288074.html
Public class mainactivity extends Activity { PrivateButton btn;PrivateNotificationmanager Manager;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Manager = (Notificationmanager) getsystemservice (Context.notification_service);//Shownotification ();//ShowNotification2 ();//ShowNotification3 ();//ShowNotification4 ();//ShowNotification5 ();ShowNotification6 (); This. btn = (Button) This. Findviewbyid (R.id.button1); This. Btn.setonclicklistener (NewOnclicklistener () {@Override Public void OnClick(View v) {clearnotification (); } }); }/** * Custom notification layouts * / Private void ShowNotification6() {Remoteviews remoteviews =NewRemoteviews (Getpackagename (), r.layout.custom_notification); Intent Intent =NewIntent ( This, Secondactivity.class); Pendingintent pendingintent = Pendingintent.getbroadcast ( This,0, Intent,0);//Send a broadcast to secondactivity when clickedRemoteviews.setonclickpendingintent (R.id.paly_pause_music, pendingintent); Intent intent1 =NewIntent ( This, Secondactivity.class); Remoteviews.setonclickpendingintent (R.id.paly_pause_music, Pendingintent.getactivity ( This,0, Intent1,0)); Notificationcompat.builder Builder =NewBuilder ( This); Builder.setcontent (remoteviews). Setsmallicon (R.drawable.ic_launcher). Setlargeicon (BitmapFactory.decodeReso Urce (Getresources (), R.drawable.ic_launcher)). Setongoing (true). Setticker ("Music is playing"); Manager.notify (0, Builder.build ()); }/** * Large layout picture Type Notification * This method seems to have a bit of a problem * * Private void ShowNotification5() {Notificationcompat.bigpicturestyle Picturestyle =NewBigpicturestyle (); Picturestyle.setbigcontenttitle ("Bigcontenttitle"). Setsummarytext ("Summary Text"). BigPicture (Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher)); Notification Notification =NewNotificationcompat.builder ( This). Setlargeicon (Bitmapfactory.decoderesource (Getresources (), R.drawable.ic_launcher)). Setticker ("Showbigview_pic"). Setcontentinfo ("ContentInfo"). Setcontenttitle ("Contenttitle"). Setcontenttext ("ContentText"). SetStyle (Picturestyle). Setautocancel (true). SetDefaults (Notification.default_all). build (); Manager.notify (3, notification); }/** * Notification bar with progress bar * / Private void ShowNotification4() {FinalNotificationcompat.builder Builder =NewNotificationcompat.builder ( This); Builder.setlargeicon (Bitmapfactory.decoderesource (Getresources (), R.drawable.ic_launcher)). SetSmallIcon ( R.drawable.ic_launcher). Setticker ("Showprogressbar"). Setcontentinfo ("ContentInfo"). Setongoing (true). Setcontenttitle ("Contenttitle"). Setcontenttext ("ContentText");NewThread (NewRunnable () {@Override Public void Run() {intProgress =0; for(progress=0;p rogress< -;p rogress+=5){//Change the third parameter to true and the progress bar will change to a style with no noticeable progressBuilder.setprogress ( -, Progress,true); Manager.notify (0, Builder.build ());Try{Thread.Sleep ( +); }Catch(Exception e) { } }Modify the display of the notification bar after the thread has finished executingBuilder.setcontenttitle ("Download Complete"). Setprogress (0,0,false). Setongoing (false); Manager.notify (0, Builder.build ()); }}). Start (); }/** * BIG Notification bar * * Private void ShowNotification3() {Notificationcompat.bigtextstyle TextStyle =NewBigtextstyle (); Textstyle.setbigcontenttitle ("Bigcontenttitle"). Setsummarytext ("Summary Text"). BigText ("I'm Big Text ... .... .... ... .... ... .... ... ..... ... ..... ..... ..... ..... ..... ..... ..... ... .. ..... ..... ..... ..... ..."); Notification Notification =NewNotificationcompat.builder ( This). Setlargeicon (Bitmapfactory.decoderesource (Getresources (), R.drawable.ic_launcher)). Setsmallicon (R.drawab Le.ic_launcher). Setticker ("Ticker"). Setcontentinfo ("ContentInfo"). Setcontenttitle ("Contenttitle"). Setcontenttext ("ContentText"). SetStyle (TextStyle). Setautocancel (true). SetDefaults (Notification.default_all). build (); Manager.notify (0, notification); }/** * Small notification bar * * Private void ShowNotification2() {Notification Notification =NewNotificationcompat.builder ( This). Setlargeicon (Bitmapfactory.decoderesource (Getresources (), R.drawable.ic_launcher)). Setsmallicon (R.drawab Le.ic_launcher). Setticker ("Shownormal"). Setcontentinfo ("ContextInfo"). Setcontenttitle ("Contenttitle"). Setcontenttext ("ContentText"). Setnumber ( $). Setautocancel (true). SetDefaults (Notification.default_all). build (); Intent notificationintent =NewIntent (mainactivity. This, Secondactivity.class); Pendingintent contentitent = pendingintent.getactivity ( This,0, Notificationintent,0); Notification.contentintent=contentitent; Manager.notify (0, notification); }/** * Show notifications in status bar * / Private void shownotification(){//Create a Notificationmanager referenceNotificationmanager Notificationmanager = (Notificationmanager) This. Getsystemservice (Android.content.Context.NOTIFICATION_SERVICE);//define various properties of notificationNotification Notification =NewNotification (R.drawable.ic_launcher,"System Test", System.currenttimemillis ());//FLAG_AUTO_CANCEL This notification can be cleared by the purge button of the status bar //flag_no_clear The notification cannot be cleared by the Purge button in the status bar //flag_ongoing_event notifications are placed in the running whether the//flag_insistent is always on, such as music always playing, know user response//Notification.flags |= notification.flag_ongoing_event;//place this notification in the "ongoing" or "running" Group of the notification bar// notification.flags |= notification.flag_no_clear;//indicates that this notification is not cleared after clicking "Clear Notification" in the notification bar, and is often used in conjunction with the Flag_ongoing_event /c1>Notification.flags |= Notification.flag_auto_cancel; Notification.flags |= notification.flag_show_lights;//default_all use all default values, such as sound, vibration, splash screen, etc. //default_lights using default flash hints //default_sounds using default cue sound //default_vibrate Use default phone shake, add <uses-permission android:name= "Android.permission.VIBRATE"/> PermissionsNotification.defaults = notification.default_lights;//Overlay Effect Constants //notification.defaults=notification.default_lights| Notification.default_sound;Notification.ledargb = Color.Blue; NOTIFICATION.LEDONMS = the;//Flash time, Ms //Set event messages for notificationsCharsequence Contenttitle ="System Messages";//Notification bar headerCharsequence ContentText ="The system is running ....";//Notification bar contentsIntent notificationintent =NewIntent (mainactivity. This, Secondactivity.class);//Click on the activity to jump after the notificationPendingintent contentitent = pendingintent.getactivity ( This,0, Notificationintent,0); Notification.setlatesteventinfo ( This, Contenttitle, ContentText, contentitent);//Pass the notification to NotificationmanagerNotificationmanager.notify (0, notification); }//Delete notification Private void clearnotification(){//Start Delete before we define the notificationNotificationmanager Notificationmanager = (Notificationmanager) This. Getsystemservice (Notification_service); Notificationmanager.cancel (0); }}
The key code article already has comments, complete code download. If you have any mistakes, you are welcome to discuss them.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.
Android Development Notification Learning notes