Multiple usages of Android notification _android

Source: Internet
Author: User

When we use the mobile phone, if the text message, and we do not click to view, is not in the top of the status bar of the mobile phone has a small message icon hint AH? Do you also want to achieve this function? Today's notification is to solve this problem.

We also know that the Android system is also escalating, there are many ways to use notification, and some methods have been discarded by Android, and now I have implemented three different methods and adapted to different versions of Android. Now I'm going to publish the code, and I like to write the explanations in the code, and I'm not going to say that anymore,

First look at the effect of the picture:

Look at the code again, the main code is as follows:

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; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  Setcontentview (R.layout.activity_main); public void NotificationMethod (view view) {//notification processing on Android, first requires a heavy system where to get Notification Manager Notificationmanager, which is a system Servic 
    E. 
    Notificationmanager manager = (Notificationmanager) getsystemservice (Context.notification_service); Switch (View.getid ()) {//default notification case R.ID.BTN1://Create a pendingintent, similar to intent, different because it is not called immediately, the need to set off in the dropdown State Bar AC Tivity, so using the pendingintent, that is, click Notification Jump to start to which activity pendingintent pendingintent = pendingintent.getactivity (this, 0, new Intent (This, MAINACTIVITY.CLA 
      SS), 0); The following are required to be compatible with the Android 2.x version of the handling mode//Notification notify1 = new Notification (r.drawable.message,//"Tickertext:" + "You have new short message, please pay attention to check!" 
      ", System.currenttimemillis ()); 
      Notification notify1 = new Notification (); 
      Notify1.icon = R.drawable.message; Notify1.tickertext = "Tickertext: You have new short message, please pay attention to check!" 
      "; 
      Notify1.when = System.currenttimemillis (); 
      Notify1.setlatesteventinfo (This, "Notification Title", "This is the Notification", 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 ID is different, then each click, add a hint to the Statu manager.notify (Notification_flag, notify1); 
    Break 
          The default notification API11 and then the available case r.id.btn2:pendingintent PendingIntent2 = pendingintent.getactivity (this, 0, NEW Intent (this, mainactivity.class), 0); Notification.builder to create notifications, note API level//API11 support Notification notify2 = new Notification.builder (thi s). Setsmallicon (R.drawable.message)//Set the small picture in the status bar, size generally recommended in 24x24, this picture is also shown in the Drop-down status bar, if there is a need to replace a larger picture, you can use the Setlargei Con (Bitmap//icon). Setticker ("Tickertext:" + "You have new short message, please check!" ")/Set the prompt text displayed on status/bar. Setcontenttitle (" Notification Title ")/Set down St ATUs//Bar after activity, the title shown in the TextView of notififymessage in this example. Setcontenttext ("This I 
          s the notification message ")//TextView the details shown in. setcontentintent (PENDINGINTENT2)//Association Pendingintent . Setnumber (1)///The number shown on the right-hand side of TextView, which can be enlarged to look at the rightmost. This number also plays the same sequence of numbers, and if multiple notifications are triggered (the same ID), you can specify which one to display. GetNotification (); Note that the build () is incremented at API level//16 and later, and Getnotificatin () can be used in API11 to replace Notify2.flags |= Notification. 
      Flag_auto_cancel; 
      Manager.notify (Notification_flag, notify2); 
    Break 
          The default notification API16 and then the available case r.id.btn3:pendingintent PendingIntent3 = pendingintent.getactivity (this, 0, 
      New Intent (this, mainactivity.class), 0); Notification.builder to create notifications, note API level//API16 support Notification notify3 = new Notification.builder (thi s). Setsmallicon (R.drawable.message). Setticker ("Tickertext: +" You have new short message, please 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.FL AGS |= 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 Manager to initiate notification. 
    If the ID is different, then each click, in the status where add a hint break; 
    customizing notificationsCase R.ID.BTN4://Notification mynotify = new Notification (r.drawable.message,//"Custom notification: You have new SMS, please check! 
      ", System.currenttimemillis ()); 
      Notification mynotify = new Notification (); 
      Mynotify.icon = R.drawable.message; Mynotify.tickertext = "Tickertext: You have new short message, please pay attention to check!" 
      "; 
      Mynotify.when = System.currenttimemillis (); 
          Mynotify.flags = notification.flag_no_clear;//is 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); 
    Break 
      Case R.ID.BTN5://Clear Notification manager.cancel (NOTIFICATION_FLAG) with ID notification_flag; Clear all notifications//manaGer.cancelall (); 
    Break 
    Default:break; 
 } 
  } 
}

Look at the main layout file again:

<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:layout"  
    _height= "Wrap_content" android:onclick= "NotificationMethod" android:text= "Default notification (has been discarded, but Universal)"/> <button Android:id= "@+id/btn2" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Androi 
    d: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"/> n android:id= "@+id/btn5" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Andro 
 id:onclick= "NotificationMethod" android:text= "purge notifications"/> </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> 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.