Android Development--Status bar notification notification, Notificationmanager detailed _android

Source: Internet
Author: User
Tags unique id

I would like to write one, but after seeing this, I would like to turn over, it is very detailed:

In the Android system, it is convenient to send a status bar notification. Let's take a look at, how to send status bar notification, status bar notification and what parameters can be set?

First, sending a status bar notification must use two classes: Notificationmanager, Notification.

Notificationmanager: Is the Status column notice management class, is responsible for sends the notice, the clear notice and so on.

Notificationmanager is a system service that must be obtained through the Getsystemservice () method.

Copy Code code as follows:
Notificationmanager nm = (Notificationmanager) getsystemservice (Notification_service);

Notification: is the specific status bar notification object, you can set icon, text, hint sound, vibration and so on parameters.

The following are the basic parameters to set a notification requirement:

An icon (notice icons)
A title and expanded message (title and content of the notice)
A pendingintent (click Notice to perform page jump)

Optional settings:

A Ticker-text Message (tip at the top of the status bar)
An alert sound (hint tone)
A vibrate setting (vibration)
A flashing LED setting (light)
Wait a minute

First, create notification

Start Notification with the Notificationmanager notify (int, Notification) method.

The first parameter uniquely identifies the notification, and the second parameter is the notification object.

II. Update of notification

Call the notification Setlatesteventinfo method to update the content, and then call the Notificationmanager Notify () method. (see the example below)

Iii. deletion of notification

Clears a notification by using the Notificationmanager cancel (int) method. Where the parameter is the unique identification ID of the notification.

Of course, you can also clear all the status bar notifications by Cancelall ().

Four, notification set (vibration, ringtones, etc.)

1. Basic settings:

New status bar notification

Copy Code code as follows:
Basenf = new Notification ();


Set the icon that the notification displays in the status bar
Copy Code code as follows:
Basenf.icon = R.drawable.icon;


What is displayed in the status bar when notified
Copy Code code as follows:
Basenf.tickertext = "You clicked basenf!";


Default parameters for notifications Default_sound, Default_vibrate, default_lights.
If you want to use all default values, use Default_all.
Default sounds are used here
Copy Code code as follows:
Basenf.defaults = Notification.default_sound;


Second parameter: The message title displayed when the Drop-down status bar expanded
Third parameter: The message content that appears when the Drop-down status bar expanded text
Fourth parameter: Click this notice to perform page jump
Copy Code code as follows:
Basenf.setlatesteventinfo (Lesson_10.this, "Title01", "Content01", PD);


Issue Status bar Notification
The parameter is the unique ID for the Notification
And the second is the Notification object.
Copy Code code as follows:
Nm.notify (Notification_id_base, BASENF);

With a picture for illustration:

2. Add sound

If you want to use the default sound, just use default.

Copy Code code as follows:
Basenf.defaults = Notification.default_sound;

If you want to use a custom sound, you'll need to use sound. As follows:

Copy Code code as follows:
Notification.sound = Uri.parse ("File:///sdcard/notification/ringer.mp3"); [/code]

The above method, using your own ringtone, if you want to use the system with the ring, you can:

[Code]notification.sound = Uri.withappendedpath (Audio.Media.INTERNAL_CONTENT_URI, "6");

Note that if default and sound are present at the same time, then the sound is invalid and the defaults ring is used.

By default, the sound of a notification is played over once. If you want the sound to loop, you need to add flag_insistent to the flags parameter. This will end up with a user response, such as a Drop-down status bar.

Copy Code code as follows:
NOTIFICATION.FLAGS |= Notification. Flag_insistent;

3. Add Vibration

If you use the default vibration method, it is also used.

Copy Code code as follows:
Notification.defaults |= notification.default_vibrate;

Of course, you can define the vibrational form yourself, and you need to use a long array here.

Copy Code code as follows:
Long[] vibrate = {0,100,200,300};
Notification.vibrate = vibrate;

In this long array, the first parameter is the time to wait before the vibration begins, the second is the time of the first vibration, the third is the time of the second vibration, and so on, and so on, arbitrarily defining how long the array is. But using this method, there is no way to repeat the vibration.

Similarly, default forms are used if default and vibrate occur simultaneously

Also need to note: The use of vibrators requires permissions, as follows:

Copy Code code as follows:
<uses-permission android:name= "Android.permission.VIBRATE" ></uses-permission>

4. Flash

Use the default lighting, as follows:

Copy Code code as follows:
Notification.defaults |= notification.default_lights;

Custom:

Copy Code code as follows:
Notification.ledargb = 0xff00ff00;
NOTIFICATION.LEDONMS = 300;
NOTIFICATION.LEDOFFMS = 1000;
Notification.flags |= notification.flag_show_lights;

where Ledargb represents the lighting color, Ledonms light duration, ledoffms dark time.

Note: This side of the color and equipment, not all colors can be, to see the specific equipment.

5. Other useful settings:

Flags

Let the sound, vibration infinite loop, until the user response

Copy Code code as follows:
Notification.flag_insistent;

When the notification is clicked, it disappears automatically.

Copy Code code as follows:
Notification.flag_auto_cancel;

Click ' Clear ', do not know the notice (QQ notice can not be cleared, is the use of this)

Copy Code code as follows:
Notification.flag_no_clear;

Here are the examples I have done for your reference. This includes creating alerts, updating notifications, clearing notifications, setting custom ringtones, customizing vibrations, customizing Notification views, and more.

Attached code:

Main class:

Package COM.YFZ; 
Import android.app.Activity; 
Import android.app.Notification; 
Import Android.app.NotificationManager; 
Import android.app.PendingIntent; 
Import android.content.Intent; 
Import Android.net.Uri; 
Import Android.os.Bundle; 
Import Android.provider.MediaStore.Audio; 
Import Android.util.Log; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.widget.Button; 
Import Android.widget.RemoteViews; 
Import Android.widget.SeekBar; 
Import Android.widget.TextView;  
 /** * Notification * @author Administrator */public class Lesson_10 extends activity {//basenotification 
  
 Private Button bt01; 
  
 Updatebasenotification private Button bt02; 
  
 Clearbasenotification private Button bt03; 
  
 Medianotification private Button bt04; 
  
 Clearmedianotification private Button bt05; 
  
 ClearAll private Button bt06; 
  
 CustomNotification private Button bt07; Notifies the manager private Notificationmanager nm; 
  
 Notification display content private pendingintent PD; 
   @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    
   /* Load Page * * Setcontentview (R.LAYOUT.LESSON10); 
 Init (); 
  private void Init () {bt01 = (Button) Findviewbyid (r.id.le10bt01); 
  bt02 = (Button) Findviewbyid (r.id.le10bt02); 
  bt03 = (Button) Findviewbyid (r.id.le10bt03); 
  bt04 = (Button) Findviewbyid (r.id.le10bt04); 
  bt05 = (Button) Findviewbyid (r.id.le10bt05); 
  bt06 = (Button) Findviewbyid (r.id.le10bt06); 
   
  bt07 = (Button) Findviewbyid (r.id.le10bt07); 
  Bt01.setonclicklistener (onclick); 
  Bt02.setonclicklistener (onclick); 
  Bt03.setonclicklistener (onclick); 
  Bt04.setonclicklistener (onclick); 
  Bt05.setonclicklistener (onclick);  
  Bt06.setonclicklistener (onclick); 
   
  Bt07.setonclicklistener (onclick); 
   
  NM = (Notificationmanager) getsystemservice (Notification_service); 
   
  Intent Intent = new Intent (this,lesson_10.class); PD = Pendingintent. Getactivity (lesson_10.this, 0, intent, 0);  } onclicklistener onclick = new Onclicklistener () {//base Notification ID private int notification_id_base 
   
  = 110; 
   
  Private Notification Basenf; 
   
  Notification ID private int notification_id_media = 119; 
   
  Private Notification medianf;  @Override public void OnClick (View v) {switch (V.getid ()) {case R.ID.LE10BT01://new status bar Notification BASENF = 
      
     New Notification (); 
      
     Set notification icon displayed in status bar Basenf.icon = R.drawable.icon; 
      
     Notice in the status bar display content Basenf.tickertext = "you clicked basenf!"; 
     Default parameters for notifications Default_sound, Default_vibrate, default_lights. 
     If you want to use all default values, use Default_all. 
     The default sound basenf.defaults |= notification.default_sound is used here. 
     Basenf.defaults |= notification.default_vibrate; 
      
     Basenf.defaults |= notification.default_lights; 
      
     Let the sound, vibration infinite cycle, until the user response basenf.flags |= notification.flag_insistent; Notification is clickedAfter the automatic disappearance of Basenf.flags |= Notification.flag_auto_cancel; 
      
      
     Click ' Clear ', do not know the notice (QQ notice can not be cleared, is the use of this) Basenf.flags |= notification.flag_no_clear;  
     Second parameter: The message header displayed when the Drop-down status bar expanded message title//third parameter: the content of the messages displayed when the Drop-down status bar expanded text//fourth parameter: Click the notification to perform page jump 
      
     Basenf.setlatesteventinfo (Lesson_10.this, "Title01", "Content01", PD); Issue status bar notification//the The parameter of the unique ID for the Notification//and the second is the Notification ob 
     Ject. 
      
     Nm.notify (Notification_id_base, BASENF); 
      
    Break 
     Case R.ID.LE10BT02://Update Notification//For example, the status bar prompts a new text message, not yet time to view, and a new message to the tip. 
     This is compared with the update of the original notification. (You can also send a notice, but this will cause confusion in the notification, and display multiple notifications to the user, not friendly to the user) Basenf.setlatesteventinfo (lesson_10.this, "Title02", "Content02" 
     , PD); 
     Nm.notify (Notification_id_base, BASENF); 
      
    Break 
     Case r.id.le10bt03://Clear Basenf Nm.cancel (notification_id_base); Break; 
     Case r.id.le10bt04:medianf = new Notification (); 
     Medianf.icon = R.drawable.icon; 
      
     Medianf.tickertext = "You clicked medianf!"; 
      
     Custom Sound Medianf.sound = Uri.withappendedpath (Audio.Media.INTERNAL_CONTENT_URI, "6"); 
     Vibration//First parameter of the notification: the time before the vibration/The second parameter: the first time of vibration, and so on long[] vir = {0,100,200,300}; 
      
     Medianf.vibrate = vir; 
      
     Medianf.setlatesteventinfo (Lesson_10.this, "Title03", "Content03", PD); 
     Nm.notify (Notification_id_media, medianf); 
      
    Break 
     Case R.ID.LE10BT05://Clear medianf Nm.cancel (Notification_id_media); 
      
    Break 
     Case R.id.le10bt06:nm.cancelall (); 
      
    Break 
     Case r.id.le10bt07://Custom Drop-down view, such as the progress bar displayed when downloading software. 
      
     Notification Notification = new Notification (); 
     Notification.icon = R.drawable.icon; 
      
     Notification.tickertext = "custom!"; Remoteviews Contentview = new Remoteviews (GETPAckagename (), r.layout.custom); 
     Contentview.setimageviewresource (R.id.image, R.drawable.icon); 
     Contentview.settextviewtext (R.id.text, "Hello, this" was in a custom expanded view); 
      
     Notification.contentview = Contentview; 
      
     When you use a custom Drop-down view, you do not need to invoke the Setlatesteventinfo () method//But you must define contentintent notification.contentintent = PD; 
     Nm.notify (3, notification); 
   Break 
  
  
} 
  } 
 }; 

 }

Home page:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical " > <button android:id= "@+id/le10bt01" android:layout_width= fill_parent "android:layout_height=" Wrap_conte NT "android:text=" Basenotification "/> <button android:id=" @+id/le10bt02 "android:layout_width=" Fill_pa Rent "android:layout_height=" wrap_content "android:text=" updatebasenotification "/> <button android:id= "@+id/le10bt03" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "ClearBaseNo" Tification "/> <button android:id= @+id/le10bt04" android:layout_width= "Fill_parent" Android:layout_hei ght= "Wrap_content" android:text= "medianotification"/> <button android:id= "@+id/le10bt05" android:layou T_width= "Fill_parent" Android:layout_height= "Wrap_content" android:text= "clearmedianotification"/> <button android:id= "@+id/le10bt06" a 
  Ndroid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "ClearALL"/> <Button Android:id= "@+id/le10bt07" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:tex  t= "CustomNotification"/> </LinearLayout>

Custom View page:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
    android:orientation=" horizontal " 
    android:layout_width=" fill_parent " 
    android:layout_" height= "Fill_parent" 
    android:padding= "3DP" 
    > 
 <imageview android:id= "@+id/image" 
    android: Layout_width= "Wrap_content" 
    android:layout_height= "fill_parent" 
    android:layout_marginright= "10DP" 
    /> 
 <textview android:id= "@+id/text" 
    android:layout_width= "Wrap_content" 
    android: layout_height= "Fill_parent" 
    android:textcolor= "#000" 
    /> 
</LinearLayout>

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.