1. 程式人生 > >【Android】狀態列通知Notification、NotificationManager詳解

【Android】狀態列通知Notification、NotificationManager詳解

在Android系統中,發一個狀態列通知還是很方便的。下面我們就來看一下,怎麼傳送狀態列通知,狀態列通知又有哪些引數可以設定?

首先,傳送一個狀態列通知必須用到兩個類:  NotificationManager 、 Notification。

NotificationManager :  是狀態列通知的管理類,負責發通知、清楚通知等。

NotificationManager 是一個系統Service,必須通過 getSystemService()方法來獲取。

[java] view plaincopyprint?
  1. NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
 

Notification:是具體的狀態列通知物件,可以設定icon、文字、提示聲音、振動等等引數。

下面是設定一個通知需要的基本引數:

  • An icon  (通知的圖示)
  • A title and expanded message  (通知的標題和內容)
  • PendingIntent   (點選通知執行頁面跳轉)

可選的設定:

  • A ticker-text message (狀態列頂部提示訊息)
  • An alert sound    (提示音)
  • A vibrate setting  (振動)
  • A flashing LED setting  (燈光)
  • 等等

一、建立Notification

通過NotificationManager  的 notify(int, Notification) 方法來啟動Notification。

   第一個引數唯一的標識該Notification,第二個引數就是Notification物件。

二、更新Notification

呼叫Notification的 setLatestEventInfo方法來更新內容,然後再呼叫NotificationManager的notify()方法即可。(具體可以看下面的例項)

三、刪除Notification

通過NotificationManager  的cancel(int)方法,來清除某個通知。其中引數就是Notification的唯一標識ID。

當然也可以通過  cancelAll() 來清除狀態列所有的通知。

四、Notification設定(振動、鈴聲等)

1. 基本設定: 

[java] view plaincopyprint?
  1. //新建狀態列通知
  2. baseNF = new Notification();  
  3. //設定通知在狀態列顯示的圖示
  4. baseNF.icon = R.drawable.icon;  
  5. //通知時在狀態列顯示的內容
  6. baseNF.tickerText = "You clicked BaseNF!";  
  7. //通知的預設引數 DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS. 
  8. //如果要全部採用預設值, 用 DEFAULT_ALL.
  9. //此處採用預設聲音
  10. baseNF.defaults = Notification.DEFAULT_SOUND;  
  11. //第二個引數 :下拉狀態列時顯示的訊息標題 expanded message title
  12. //第三個引數:下拉狀態列時顯示的訊息內容 expanded message text
  13. //第四個引數:點選該通知時執行頁面跳轉
  14. baseNF.setLatestEventInfo(Lesson_10.this"Title01""Content01", pd);  
  15. //發出狀態列通知
  16. //The first parameter is the unique ID for the Notification 
  17. // and the second is the Notification object.
  18. nm.notify(Notification_ID_BASE, baseNF);  

配一張圖作說明:

2. 新增聲音

如果要採用預設聲音,只要使用default就可以了。

[java] view plaincopyprint?
  1. baseNF.defaults = Notification.DEFAULT_SOUND;  
 

如果要使用自定義聲音,那麼就要用到sound了。如下:

[java] view plaincopyprint?
  1. notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");  
 

上面這種方法,使用的是自己的鈴聲,如果想用系統自帶的鈴聲,可以這樣:

[java] view plaincopyprint?
  1. notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");  
 

需要注意一點,如果default、sound同時出現,那麼sound無效,會使用預設鈴聲。

預設情況下,通知的聲音播放一遍就會結束。 如果你想讓聲音迴圈播放,需要為flags引數加上FLAG_INSISTENT。 這樣聲音會到使用者響應才結束,比如下拉狀態列。

[java] view plaincopyprint?
  1. notification.flags |= notification.FLAG_INSISTENT;  
 

3. 新增振動

如果是使用預設的振動方式,那麼同樣也是使用default。

[java] view plaincopyprint?
  1. notification.defaults |= Notification.DEFAULT_VIBRATE;  
 

當然也可以自己定義振動形式,這邊需要用到Long型陣列。 

[java] view plaincopyprint?
  1. long[] vibrate = {0,100,200,300};  
  2. notification.vibrate = vibrate;  
 

這邊的Long型陣列中,第一個引數是開始振動前等待的時間,第二個引數是第一次振動的時間,第三個引數是第二次振動的時間,以此類推,隨便定義多長的陣列。但是採用這種方法,沒有辦法做到重複振動。

同樣,如果default、vibrate同時出現時,會採用預設形式。

另外還需要注意一點:使用振動器時需要許可權,如下:

[xhtml] view plaincopyprint?
  1. <uses-permissionandroid:name="android.permission.VIBRATE"></uses-permission>
 

4. 閃光

使用預設的燈光,如下:

[java] view plaincopyprint?
  1. notification.defaults |= Notification.DEFAULT_LIGHTS;  

自定義

[java] view plaincopyprint?
  1. notification.ledARGB = 0xff00ff00;  
  2. notification.ledOnMS = 300;  
  3. notification.ledOffMS = 1000;  
  4. notification.flags |= Notification.FLAG_SHOW_LIGHTS;  

其中ledARGB 表示燈光顏色、 ledOnMS 亮持續時間、ledOffMS 暗的時間。

注意:這邊的顏色跟裝置有關,不是所有的顏色都可以,要看具體裝置。

5. 其他有用的設定:

flags

Notification.FLAG_INSISTENT;  //讓聲音、振動無限迴圈,直到使用者響應

Notification.FLAG_AUTO_CANCEL;    //通知被點選後,自動消失

Notification.FLAG_NO_CLEAR;  //點選'Clear'時,不清楚該通知(QQ的通知無法清除,就是用的這個)

下面附上我做的例子,供大家參考。 裡面包括建立通知、更新通知、清除通知、設定自定義鈴聲、自定義振動、自定義通知檢視等。

  

附上程式碼:

主類:

[java] view plaincopyprint?
  1. package com.yfz;  
  2. import android.app.Activity;  
  3. import android.app.Notification;  
  4. import android.app.NotificationManager;  
  5. import android.app.PendingIntent;  
  6. import android.content.Intent;  
  7. import android.net.Uri;  
  8. import android.os.Bundle;  
  9. import android.provider.MediaStore.Audio;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.RemoteViews;  
  15. import android.widget.SeekBar;  
  16. import android.widget.TextView;  
  17. /** 
  18.  * Notification 
  19.  * @author Administrator 
  20.  * 
  21.  */
  22. publicclass Lesson_10 extends Activity {  
  23.     //BaseNotification
  24.     private Button bt01;  
  25.     //UpdateBaseNotification
  26.     private Button bt02;  
  27.     //ClearBaseNotification
  28.     private Button bt03;  
  29.     //MediaNotification
  30.     private Button bt04;  
  31.     //ClearMediaNotification
  32.     private Button bt05;  
  33.     //ClearALL
  34.     private Button bt06;  
  35.     //CustomNotification
  36.     private Button bt07;  
  37.     //通知管理器
  38.     private NotificationManager nm;  
  39.     //通知顯示內容
  40.     private PendingIntent pd;  
  41.     @Override
  42.      publicvoid onCreate(Bundle savedInstanceState) {  
  43.             super.onCreate(savedInstanceState);  
  44.             /*載入頁面*/
  45.             setContentView(R.layout.lesson10);  
  46.             init();  
  47.     }  
  48.     privatevoid init() {  
  49.         bt01 = (Button)findViewById(R.id.le10bt01);  
  50.         bt02 = (Button)findViewById(R.id.le10bt02);  
  51.         bt03 = (Button)findViewById(R.id.le10bt03);  
  52.         bt04 = (Button)findViewById(R.id.le10bt04);  
  53.         bt05 = (Button)findViewById(R.id.le10bt05);  
  54.         bt06 = (Button)findViewById(R.id.le10bt06);  
  55.         bt07 = (Button)findViewById(R.id.le10bt07);  
  56.         bt01.setOnClickListener(onclick);  
  57.         bt02.setOnClickListener(onclick);  
  58.         bt03.setOnClickListener(onclick);  
  59.         bt04.setOnClickListener(onclick);  
  60.         bt05.setOnClickListener(onclick);  
  61.         bt06.setOnClickListener(onclick);     
  62.         bt07.setOnClickListener(onclick);  
  63.         nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  64.         Intent intent = new Intent(this,Lesson_10.class);  
  65.         pd = PendingIntent.getActivity(Lesson_10.this0, intent, 0);  
  66.     }  
  67.     OnClickListener onclick = new OnClickListener() {  
  68.         //BASE Notification ID
  69.         privateint Notification_ID_BASE = 110;  
  70.         private Notification baseNF;  
  71.         //Notification ID
  72.         privateint Notification_ID_MEDIA = 119;  
  73.         private Notification mediaNF;  
  74.         @Override
  75.         publicvoid onClick(View v) {  
  76.             switch(v.getId()) {  
  77.                 case R.id.le10bt01:  
  78.                     //新建狀態列通知
  79.                     baseNF = new Notification();  
  80.                     //設定通知在狀態列顯示的圖示
  81.                     baseNF.icon = R.drawable.icon;  
  82.                     //通知時在狀態列顯示的內容
  83.                     baseNF.tickerText = "You clicked BaseNF!";  
  84.                     //通知的預設引數 DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS. 
  85.                     //如果要全部採用預設值, 用 DEFAULT_ALL.
  86.                     //此處採用預設聲音
  87.                     baseNF.defaults |= Notification.DEFAULT_SOUND;  
  88.                     baseNF.defaults |= Notification.DEFAULT_VIBRATE;  
  89.                     baseNF.defaults |= Notification.DEFAULT_LIGHTS;  
  90.                     //讓聲音、振動無限迴圈,直到使用者響應
  91.                     baseNF.flags |= Notification.FLAG_INSISTENT;  
  92.                     //通知被點選後,自動消失
  93.                     baseNF.flags |= Notification.FLAG_AUTO_CANCEL;  
  94.                     //點選'Clear'時,不清楚該通知(QQ的通知無法清除,就是用的這個)
  95.                     baseNF.flags |= Notification.FLAG_NO_CLEAR;  
  96.                     //第二個引數 :下拉狀態列時顯示的訊息標題 expanded message title
  97.                     //第三個引數:下拉狀態列時顯示的訊息內容 expanded message text
  98.                     //第四個引數:點選該通知時執行頁面跳轉
  99.                     baseNF.setLatestEventInfo(Lesson_10.this"Title01""Content01", pd);  
  100.                     //發出狀態列通知
  101.                     //The first parameter is the unique ID for the Notification 
  102.                     // and the second is the Notification object.
  103.                     nm.notify(Notification_ID_BASE, baseNF);  
  104.                     break;  
  105.                 case R.id.le10bt02:  
  106.                     //更新通知
  107.                     //比如狀態列提示有一條新簡訊,還沒來得及檢視,又來一條新簡訊的提示。
  108.                     //此時採用更新原來通知的方式比較。
  109.                     //(再重新發一個通知也可以,但是這樣會造成通知的混亂,而且顯示多個通知給使用者,對使用者也不友好)
  110.                     baseNF.setLatestEventInfo(Lesson_10.this"Title02""Content02", pd);  
  111.                     nm.notify(Notification_ID_BASE, baseNF);  
  112.                     break;  
  113.                 case R.id.le10bt03:  
  114.                     //清除 baseNF
  115.                     nm.cancel(Notification_ID_BASE);  
  116.                     break;  
  117.                 case R.id.le10bt04:  
  118.                     mediaNF = new Notification();  
  119.                     mediaNF.icon = R.drawable.icon;  
  120.                     mediaNF.tickerText = "You clicked MediaNF!";  
  121.                     //自定義聲音
  122.                     mediaNF.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");  
  123.                     //通知時發出的振動
  124.                     //第一個引數: 振動前等待的時間
  125.                     //第二個引數: 第一次振動的時長、以此類推
  126.                     long[] vir = {0,100,200,300};  
  127.                     mediaNF.vibrate = vir;  
  128.                     mediaNF.setLatestEventInfo(Lesson_10.this"Title03""Content03", pd);  
  129.                     nm.notify(Notification_ID_MEDIA, mediaNF);  
  130.                     break;  
  131.                 case R.id.le10bt05:  
  132.                     //清除 mediaNF
  133.                     nm.cancel(Notification_ID_MEDIA);  
  134.                     break;  
  135.                 case R.id.le10bt06:  
  136.                     nm.cancelAll();  
  137.                     break;  
  138.                 case R.id.le10bt07:  
  139.                     //自定義下拉檢視,比如下載軟體時,顯示的進度條。
  140.                     Notification notification = new Notification();  
  141.                     notification.icon = R.drawable.icon;  
  142.                     notification.tickerText = "Custom!";  
  143.                     RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom);  
  144.                     contentView.setImageViewResource(R.id.image, R.drawable.icon);  
  145.                     contentView.setTextViewText(R.id.text, "Hello, this message is in a custom expanded view");  
  146.                     notification.contentView = contentView;  
  147.                     //使用自定義下拉檢視時,不需要再呼叫setLatestEventInfo()方法
  148.                     //但是必須定義 contentIntent
  149.                     notification.contentIntent = pd;  
  150.                     nm.notify(3, notification);  
  151.                     break;  
  152.             }  
  153.         }  
  154.     };  
  155. }  

主頁面:

[xhtml] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayout
  3.   xmlns:android="http://schemas.android.com/apk/res/android"
  4.   android:layout_width="fill_parent"
  5.   android:layout_height="fill_parent"
  6.   android:orientation="vertical">
  7.     <Button
  8.         android:id="@+id/le10bt01"
  9.         android:layout_width="fill_parent"
  10.         android:layout_height="wrap_content"
  11.         android:text="BaseNotification"
  12.     />
  13.     <Button
  14.         android:id="@+id/le10bt02"
  15.         android:layout_width="fill_parent"
  16.         android:layout_height="wrap_content"
  17.         android:text="UpdateBaseNotification"
  18.     />
  19.     <Button
  20.         android:id="@+id/le10bt03"
  21.         android:layout_width="fill_parent"
  22.         android:layout_height="wrap_content"
  23.         android:text="ClearBaseNotification"
  24.     />
  25.     <Button
  26.         android:id="@+id/le10bt04"
  27.         android:layout_width="fill_parent"
  28.         android:layout_height="wrap_content"
  29.         android:text="MediaNotification"
  30.     />
  31.     <Button
  32.         android:id="@+id/le10bt05"
  33.         android:layout_width="fill_parent"
  34.         android:layout_height="wrap_content"
  35.         android:text="ClearMediaNotification"
  36.     />
  37.     <Button
  38.         android:id="@+id/le10bt06"
  39.         android:layout_width="fill_parent"
  40.         android:layout_height="wrap_content"
  41.         android:text="ClearALL"
  42.     />
  43.     <Button
  44.         android:id="@+id/le10bt07"
  45.         android:layout_width="fill_parent"
  46.         android:layout_height="wrap_content"
  47.         android:text="CustomNotification"
  48.     />
  49. </LinearLayout>

自定義檢視頁面:

[c-sharp] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.               android:orientation="horizontal"
  4.               android:layout_width="fill_parent"
  5.               android:layout_height="fill_parent"
  6.               android:padding="3dp"
  7.               >  
  8.     <ImageView android:id="@+id/image"
  9.               android:layout_width="wrap_content"
  10.               android:layout_height="fill_parent"
  11.               android:layout_marginRight="10dp"
  12.               />  
  13.     <TextView android:id="@+id/text"
  14.               android:layout_width="wrap_content"
  15.               android:layout_height="fill_parent"
  16.               android:textColor="#000"
  17.               />  
  18. </LinearLayout>