Multiple uses of Android notification (RPM)

Source: Internet
Author: User

When we use the mobile phone, if the 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:

 Packagenet.loonggg.notification;Importandroid.app.Activity;Importandroid.app.Notification;ImportAndroid.app.NotificationManager;Importandroid.app.PendingIntent;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;Importandroid.widget.RemoteViews; Public classMainactivityextendsActivity {Private Static Final intNotification_flag = 1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidNotificationMethod (view view) {//on Android for notification processing, first you need to re-system where to get Notification Manager Notificationmanager, which is a system service. Notificationmanager Manager =(Notificationmanager) Getsystemservice (Context.notification_service); Switch(View.getid ()) {//Default Notification         CaseR.ID.BTN1://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 new short message, please pay attention to check!" ", System.currenttimemillis ());Notification notify1 =NewNotification (); Notify1.icon=R.drawable.message; Notify1.tickertext= "Tickertext: You have a new short message, please note check!" "; Notify1.when=System.currenttimemillis (); Notify1.setlatesteventinfo ( This, "Notification Title",                    "This is 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 to the Statumanager.notify (Notification_flag, notify1);  Break; //default Notification API11 and later available         Caser.id.btn2: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.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 replace a larger picture, you can use Setlargeicon (Bitmap //icon). Setticker ("Tickertext:" + "You have a new short message, please pay attention to check!") ")//Set in Status//hint text displayed on bar. Setcontenttitle ("Notification Title")//Setting the dropdown status//Bar post activity, the title 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 ();//Be aware that build () is at the API level//16 and later added, in API11 can use Getnotificatin () to replaceNotify2.flags |=Notification.flag_auto_cancel;            Manager.notify (Notification_flag, notify2);  Break; //default Notification API16 and later available         Caser.id.btn3:pendingintent PendingIntent3= Pendingintent.getactivity ( This, 0,                    NewIntent ( This, Mainactivity.class), 0); //Create notifications with Notification.builder, note API level//supported after API16 .Notification Notify3 =NewNotification.builder ( This). 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 ();//It is important to note that build () is in the API//Level16 and later added, 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             Break; //Custom Notifications         CaseR.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 =NewNotification (); Mynotify.icon=R.drawable.message; 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);  Break;  Caser.id.btn5://clear notifications with ID Notification_flagManager.cancel (Notification_flag); //Clear All Notifications//Manager.cancelall ();             Break; default:             Break; }    }}

<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:orientation= "Vertical"Tools:context=". Mainactivity "> <Button Android:id= "@+id/btn1"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:onclick= "NotificationMethod"Android:text= "Default notification (abandoned, 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= "NotificationMethod"Android:text= "Default notification (available after API16)"/> <Button Android:id= "@+id/btn4"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:onclick= "NotificationMethod"Android:text= "Custom Notification"/> <Button Android:id= "@+id/btn5"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:onclick= "NotificationMethod"Android:text= "Purge Notification"/></linearlayout>

Another is: Custom notification layout file My_notification.xml, the code is as follows:

<?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 >

http://blog.csdn.net/loongggdroid/article/details/17616509

Multiple uses of Android notification (RPM)

Related Article

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.