Notification is divided into ordinary notification and custom two, by the icon, ticker (hint information), title, content, time, event composition,
General Notice Demo:
1 package com.android.hzynotification; 2 3 Import android.app.Activity; 4 Import android.app.Notification; 5 Import Android.app.NotificationManager; 6 Import android.app.PendingIntent; 7 Import Android.content.Context; 8 Import android.content.Intent; 9 Import android.os.bundle;10 Import android.view.view;11 public class Mainactivity extends Activity {@Overri De15 protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); setc Ontentview (r.layout.activity_main);}19 public void Send (View v) {//compatible with old version (2.3) 21//1. Get Notification Manager 22 Notificationmanager nm = (Notificationmanager) getsystemservice (context.notification_service); 23//2. Build NotificationsThe time that the icon flashes to show the content received Notification Notification = new Notification (Android. R.drawable.stat_notify_chat,"This is a message",System.currenttimemillis ()); 25//3. Set Notification Click event Intent Intent = new Intent (this,mainactivity.class); Create an explicit intent of pendingintent contentintent = pendingintent.getactivity(This, intent, 0); //the activity that is opened after a click can also be either server or BoardcastNotification.setlatesteventinfo (This, "the title of the notice", "the content of the notice", contentintent); notification.flags = Notification.flag_auto_cancel; Automatically disappears after you click Notifications 31 32//4. Send notifications to Nm.notify (Process.mypid (), notification); Process.mypid (); Gets the id34 of the current process}35}
Customize the layout settings for notifications:
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "3 android:layout_width=" match_parent "4 android:layout_height=" match_parent "5 android:orientation=" H Orizontal "> 6 7 <imageview 8 android:layout_width=" wrap_content "9 android:layout_height= "Wrap_content" Ten android:src= "@drawable/ic_launcher"/>12 <linearlayout out_marginleft= "3DP" android:layout_width= "match_parent" android:layout_height= "Wrap_content" 16 android:orientation= "vertical" >18 <textview android:layout_margintop= "2DP" 20 Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:text = "Custom notifications, professional interceptors"/>24 <progressbar android:layout_margintop= "2DP" 26 Android:layout_width= "Match_pArent "android:layout_height=" wrap_content "style=" @android: Style/widget.progressbar.horizonta L "/>30 </linearlayout>31 </LinearLayout>
The custom notification code is as follows:
1 package com.android.hzynotification; 2 Import android.app.Activity; 3 Import android.app.Notification; 4 Import Android.app.NotificationManager; 5 Import android.app.PendingIntent; 6 Import Android.content.Context; 7 Import android.content.Intent; 8 Import Android.os.Bundle; 9 Import android.view.view;10 Import android.widget.remoteviews;11 public class Mainactivity extends Activity {13 14 @Override15 protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 17 Setcontentview (r.layout.activity_main);}19 public void Costom (View v) {22//1. Get Notification Manager 23 Notificationmanager nm = (Notificationmanager) getsystemservice (context.notification_service); 24//2. Build Notifications Notification Notification = new Notification (); Notification.icon = Android. R.drawable.stat_notify_missed_call; Icon Notification.tickertext = "There are missed calls oh"; Remoteviews Contentview = nEW Remoteviews (Getpackagename (), r.layout.content); notification.contentview = Contentview; Set the layout of the notification display 31 32//Set Notification Click event Intent Intent = new Intent (this,otheractivity.class); Endingintent contentintent = pendingintent.getactivity (this, intent, 0); notification.contentintent = Conte NTINTENT;36 37//set to automatically disappear after clicking notifications notification.flags = notification.flag_auto_cancel;39//Hair Notice nm.notify (notification); 41 42}43}
Go Notification status bar Notification