ANDROID Notification Notification resolution _ANDROID

Source: Internet
Author: User

Notification is displayed in the mobile status bar of the notice, notification notification is a global notice, generally through the notificationmanager to manage.
The general use of notification steps are as follows:

    • 1. Call Getsysytemservice (Notification_service) to obtain the Notificationmanager of the system for NOTIFICATION sending and recycling
    • 2. Build a notification through the constructor
    • 3. Set various properties for notification, then Builder ()
    • 4. Send notification via Notificationmanager

Here is an example to illustrate the use of the above, first look at a picture

I. Access to System Notificationmanager

Private Notificationmanager nm;

  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);
    Get notification management for the system
    NM = (Notificationmanager) getsystemservice (Notification_service);
  }

Two. Add a listener event for the two buttons of the main layout, and then set the startup notification separately, setting various properties and canceling notifications
the various attribute codes are described in detail and can be referenced in the API

Start notification

 public void Send (view view) {//used to open notification to start another activity Intent Intent = new Intent (mainactivi
    Ty.this,otheractivity.class);
    For delayed start pendingintent pi = pendingintent.getactivity (mainactivity.this, 0, intent, 0);
        Set notification Notification notify = new Notification.builder (this)//setting opens the notification and the notification disappears automatically. Setautocancel (True) Sets the notification prompt message displayed in the status bar. Setticker ("new Message")//Set the notification bar icon. Setsmallicon (r.mipmap.ic_launcher)/Set The title of the notification content. Setcontenttitle ("A new Notice")//Set up the notification content. Setcontenttext ("Congratulations on your notification bar Test Success")//Set Use system default sound, default LED Lamps-SetDefaults (Notification.default_sound |
        Notification.default_lights)//all words are all using the default, sound, vibration, flash, need to add appropriate permissions//. SetDefaults (All)/or custom sound
    Setsound (Uri.parse ())//Set the program to be started. Setcontentintent (PI)//finally build to create the notification. builds ();
  Send the current notification, through Notificationmanager to manage nm.notify (1,notify); }

The otheractivity used here is another activity that is initiated by the notification, in order to initiate the need to include this activity in the manifest file, and because the flash and vibrator are used, the appropriate permissions need to be added

<activity android:name= ". Otheractivity "> </activity>
  <uses-permission android:name=" Android.permission.FLASHLIGHT "/>
  <uses-permission android:name= "Android.permission.VIBRATE"/>

Cancel Notification

Cancel notifies public
  void closed (view view) {
    nm.cancel (1);
  }

It's quite handy to use. Finally attach the main interface layout

 <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:paddingleft=" @dimen/activity_horizontal_margin "android:orientation=" Horizontal "android:paddingRight=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_vertical_margin "android:paddingbottom=" @ Dimen/activity_vertical_margin "tools:context=". Mainactivity "> <button android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Andr" oid:text= "Turn on Notification" android:onclick= "send" android:id= "@+id/btnstartnotification"/> <button Android 
    : layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "Close Notification" android:onclick= "closed" Android:id= "@+id/btnstopnotification"/> </LinearLayout> 

The above is about the details of the Android notification notice, I hope to help you learn.

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.