Notification use in different versions of the SDK

Source: Internet
Author: User

Today summed up the use of notification, found that the control in the version is a bit of a drag. API11 and API16 Two watershed, leading to rookie in the study time is difficult, the affected external factors are diverse, which is more important is the version of the Android device you tested, I just started to use the Android version of android4.0.4, I used in the program is API16 above methods, just android4.0 the corresponding API for API15, a difference between a version, resulting in notification when the program hangs.

If you want to be compatible with the lower version of the Android system (API11 below), it is recommended to use the method:

Notificationmanager Manager =(Notificationmanager) Getsystemservice (context.notification_service);//creating a pendingintent, similar to intent, is different because it is not called immediately and requires activity in the drop-down status bar, so the pendingintent is used, That is, click Notification Jump to start to which activityPendingintent pendingintent = pendingintent.getactivity ( This, 0,NewIntent ( This, Mainactivity.class), 0);//The following is compatible with the Android 2.x version is the processing method//Notification notify1 = new Notification (R.drawable.message, "Tickertext:" + "You have a short message, please pay attention to check!") ", System.currenttimemillis ());Notification notify1 =NewNotification (); Notify1.icon=R.drawable.ic_launcher;notify1.tickertext= "Tickertext: You have a new short message, please note check!" "; Notify1.number= 2; Notify1.when=System.currenttimemillis (); Notify1.setlatesteventinfo ( This, "Notification Title", "This is the Notification message", pendingintent); Notify1.flags|= notification.flag_no_clear;//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 to the StatuManager.notify (Notification_flag, notify1);

If you are compatible with Android3.0 and above (API11) systems, use the following methods:

Notificationmanager Manager =(Notificationmanager) Getsystemservice (Context.notification_service); Pendingintent PendingIntent2= Pendingintent.getactivity ( This, 0,NewIntent ( This, Mainactivity.class), 0);//Create notifications with Notification.builder, note API level//supported after API11 .Notification notify2 =NewNotification.builder ( This). Setsmallicon (R.drawable.ic_launcher)//set a 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 replace the larger picture, you can use Setlargeicon (Bitmap icon). Setticker ("Tickertext:" + "You have a new short message, please pay attention to check!") ")//set the hint text that appears on the status bar. Setcontenttitle ("Notification Title")//setting the activity after the drop status bar, the caption shown in the TextView of notififymessage in this example. Setcontenttext ("This is the notification message")//details displayed in the TextView. Setcontentintent (PendingIntent2)//Association Pendingintent. Setnumber (1)//The number shown on the right side of the TextView can be enlarged to look at the rightmost image. 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 incremented at API level 16 and then can be used in API11 instead of Getnotificatin () .Notify2.flags |=notification.flag_auto_cancel;manager.notify (Notification_flag, notify2);

If you only consider Android4.1 and above, please use the following method better

Notificationmanager Manager =(Notificationmanager) Getsystemservice (context.notification_service);//default Notification API16 and later availablePendingintent PendingIntent3 = pendingintent.getactivity ( This, 0,NewIntent ( This, Mainactivity.class), 0);//Create notifications with Notification.builder, note API level (API16 support)Notification Notify3 =NewNotification.builder ( This). Setsmallicon (R.drawable.ic_launcher). 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 the API Level16 and after, API11 can use Getnotificatin () to replaceNotify3.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: Initiate notifications through the notification Manager. If the ID is different, then each click adds a hint to the status where

Custom interface notification, this interface is a bit simpler (there is only one textview).

Notificationmanager Manager =(Notificationmanager) Getsystemservice (Context.notification_service); Notification mynotify=NewNotification (); Mynotify.icon=R.drawable.ic_launcher;mynotify.tickertext= "Tickertext: You have a new short message, please note check!" "; Mynotify.when=System.currenttimemillis (); Mynotify.flags= Notification.flag_no_clear;//can not automatically clearRemoteviews RV =Newremoteviews (Getpackagename (), r.layout.my_notification); Rv.settextviewtext (R.id.text_content,"Hello wrold!"); Mynotify.contentview=rv;intent Intent=NewIntent (Intent.action_main); Pendingintent contentintent= Pendingintent.getactivity ( This, 1, intent, 1); Mynotify.contentintent=contentintent;manager.notify (Notification_flag, mynotify);

We also need to clear the notification when necessary, if the notification has been shown that the words are always not too good

// clear notifications with ID Notification_flag Manager.cancel (notification_flag); // Clear All Notifications Manager.cancelall ();

Reference URL: Multiple uses of Android notification: http://blog.csdn.net/loongggdroid/article/details/17616509

Notification use in different versions of the SDK

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.