When we use the mobile phone, if a text message, and we do not click to see, is not in the top of the mobile phone status bar has a small text icon hint ah? Do you want to implement this function? Today's notification is to solve this problem.
We also know that the Android system is also constantly upgrading, there are many ways to use the notification, some methods have been abandoned by Android, now I implemented three different methods, and adapt to different versions of Android. Now I'm going to publish the code, I like to write the explanation in the code, here I don't say, first look:
Look at the code again, the main code is as follows:
<span style= "FONT-SIZE:18PX;" >package Net.loonggg.notification;import Android.app.activity;import Android.app.notification;import Android.app.notificationmanager;import Android.app.pendingintent;import Android.content.context;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.widget.RemoteViews;public Class Mainactivity extends Activity {private static final int notification_flag = 1; @Overrideprotected void OnCreate (Bundl E savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void NotificationMethod (view view) {////on Android for notification processing, first requires heavy system where to get Notification Manager Notificationmanager, which is a system service. Notificationmanager manager = (Notificationmanager) getsystemservice (context.notification_service); switch ( View.getid ()) {///default notification case r.id.btn1://creates a pendingintent similar to intent, unlike an activity that requires a drop-down status bar because it is not called immediately. So the use of pendingintent, that is, click Notification Jump to boot to which activitypendingintent pendingintent = pendingintent.getactivity (thIS, 0,new Intent (this, mainactivity.class), 0);//The following is compatible with the Android 2.x version is the processing mode//Notification notify1 = new Notification (r.dr awable.message,//"Tickertext:" + "You have a new short message, please pay attention to check! ", System.currenttimemillis ()); Notification notify1 = new Notification () Notify1.icon = R.drawable.message;notify1.tickertext = "Tickertext: You have a new short message, Please pay attention to checking! "; notify1.when = System.currenttimemillis (); Notify1.setlatesteventinfo (This," Notification Title "," the Notification message ", pendingintent); notify1.number = 1;notify1.flags |= notification.flag_auto_cancel; Flag_auto_cancel indicates that the notification will be cleared when the notification is clicked by the user. Notifications are initiated by the notification Manager. If the IDs are different, then each click adds a hint manager.notify (Notification_flag, notify1) to the Statu, break;//default notification API11 and then case R.ID.BTN2: Pendingintent PendingIntent2 = pendingintent.getactivity (This, 0,new Intent (this, mainactivity.class), 0);//Through <a Target=_blank name= "Baidusnap1" ></a>notification.builder to create notifications, note that API level//API11 is supported after Notification Notify2 = new Notification.builder (this). Setsmallicon (R.drawable.message)//Set the small picture in the status bar, the size is generally recommended in 24x24, this picture is also shown in the dropdown status bar, if you need to change a larger picture there, you can use Setlargeicon (bitmap//icon). Setticker (" Tickertext: "+" You have a new short message, please pay attention to check! ")//Set the prompt text displayed on status//bar. Setcontenttitle (" Notification Title ")//Set activity after drop status//bar, This example shows the caption in the textview of Notififymessage. Setcontenttext ("This is the notification message")// The details displayed in the TextView. Setcontentintent (PENDINGINTENT2)//Associated Pendingintent.setnumber (1)//On the right side of the TextView shows the number, can enlarge the picture to see, on the rightmost. This number also plays to the left and right of the serial numbers, and if more than one notification is triggered (the same ID), you can specify which one to display: GetNotification (); It is important to note that build () is added after API level//16, and in API11, Getnotificatin () can be used instead of notify2.flags |= Notification.flag_auto_cancel; Manager.notify (Notification_flag, notify2); break;//default notification API16 and later available case R.id.btn3:pendingintent PendingIntent3 = Pendingintent.getactivity (This, 0,new Intent (this, mainactivity.class), 0);//create notifications via Notification.builder, note API Notification notify3 = new Notification.builder (this) is only supported after level//API16. Setsmallicon (R.drawable.message). Setticker ( "Tickertext:" + "You have a new short message, please pay attention to check!" "). SetcontenTtitle ("Notification Title"). Setcontenttext ("This is the Notification message"). Setcontentintent (PENDINGINTENT3). Setnumber (1). build (); Note that build () is added after api//level16 and API11 can use Getnotificatin () instead of Notify3.flags |= Notification.flag_auto_cancel; Flag_auto_cancel indicates that the notification will be cleared when the notification is clicked by the user. Manager.notify (Notification_flag, notify3);////Step 4: Notify by Notification Manager. If the ID is different, then each click, where the status adds a hint break;//the custom notification case r.id.btn4://Notification mynotify = new Notification ( r.drawable.message,//"Custom notification: You have a new message, please pay attention to check!" ", System.currenttimemillis ()); Notification mynotify = new Notification () Mynotify.icon = R.drawable.message;mynotify.tickertext = "Tickertext: You have a new SMS message , please pay attention to checking! "; mynotify.when = System.currenttimemillis (); mynotify.flags = notification.flag_no_clear;//not able to automatically clear remoteviews RV = New Remoteviews (Getpackagename (), r.layout.my_notification); Rv.settextviewtext (r.id.text_content, "Hello wrold!"); Mynotify.contentview = rv;intent Intent = new Intent (intent.action_main); Pendingintent contentintent = Pendingintent.getaCtivity (This, 1,intent, 1); mynotify.contentintent = Contentintent;manager.notify (Notification_flag, myNotify); Case r.id.btn5://clears the notification ID of Notification_flag manager.cancel (Notification_flag);//Clears all notifications//Manager.cancelall (); Break;default:break;}}} </span>look at the main layout file again:
<span style= "FONT-SIZE:18PX;" ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "Vertical" tools:context= ". Mainactivity "> <button android:id=" @+id/btn1 "android:layout_width=" Fill_parent "Android:la yout_height= "Wrap_content" android:onclick= "NotificationMethod" android:text= "default notification (discarded, but generic)"/> < Button android:id= "@+id/btn2" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:onclick= "NotificationMethod" android:text= "default notification (available after API11)"/> <button android:id= " @+id/btn3 "android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:onClick=" n Otificationmethod "android:text=" default notification (available after API16) "/> <button android:id="@+id/btn4 "android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:onClick=" n Otificationmethod "android:text=" Custom Notification "/> <button android:id=" @+id/btn5 "Android:layout_wid Th= "Fill_parent" android:layout_height= "wrap_content" android:onclick= "NotificationMethod" Android:te xt= "purge Notification"/></linearlayout></span>
Another is: Custom notification layout file My_notification.xml, the code is as follows:
<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android " android:layout_width=" match_parent " android:layout_height=" Wrap_content " android: Background= "#ffffff" android:orientation= "vertical" > <textview android:id= "@+id/text_content " android:layout_width=" wrap_content " android:layout_height=" wrap_content " android:textsize=" 20SP " /></linearlayout></span>
Android usage notes for notification under different APIs