Android 狀態欄通知Notification、NotificationManager詳解


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

 

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

 

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

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

 

1 <code>NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);</code>

 

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. 基本設置:

01 //新建狀態欄通知
02 baseNF = new Notification();
03   
04 //設置通知在狀態欄顯示的圖標
05 baseNF.icon = R.drawable.icon;
06  
07 //通知時在狀態欄顯示的內容
08 baseNF.tickerText = "You clicked BaseNF!";
09  
10 //通知的默認參數 DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS.
11 //如果要全部采用默認值, 用 DEFAULT_ALL.
12 //此處采用默認聲音
13 baseNF.defaults = Notification.DEFAULT_SOUND;
14  
15 //第二個參數 :下拉狀態欄時顯示的消息標題 expanded message title
16 //第三個參數:下拉狀態欄時顯示的消息內容 expanded message text
17 //第四個參數:點擊該通知時執行頁面跳轉
18 baseNF.setLatestEventInfo(Lesson_10.this, "Title01", "Content01", pd);
19  
20 //發出狀態欄通知
21 //The first parameter is the unique ID for the Notification
22 // and the second is the Notification object.
23 nm.notify(Notification_ID_BASE, baseNF);

配一張圖作說明:

2. 添加聲音

如果要采用默認聲音,只要使用default就可以了。

1 baseNF.defaults = Notification.DEFAULT_SOUND;

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

1 notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

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

1 notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

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

 

  默認情況下,通知的聲音播放一遍就會結束。 如果你想讓聲音循環播放,需要為flags參數加上FLAG_INSISTENT。 這樣聲音會到用戶響應才結束,比如下拉狀態欄。

1 notification.flags |= notification.FLAG_INSISTENT;

3. 添加振動

如果是使用默認的振動方式,那么同樣也是使用default。

1 notification.defaults |= Notification.DEFAULT_VIBRATE;

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

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

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

 

同樣,如果default、vibrate同時出現時,會采用默認形式。

 

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

1 <uses-permission android:name="android.permission.VIBRATE"></uses-permission>

4. 閃光

 使用默認的燈光,如下:

1 notification.defaults |= Notification.DEFAULT_LIGHTS;

自定義

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的通知無法清除,就是用的這個)


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


附上代碼:

 

主類:

001 package com.yfz;
002 import android.app.Activity;
003 import android.app.Notification;
004 import android.app.NotificationManager;
005 import android.app.PendingIntent;
006 import android.content.Intent;
007 import android.net.Uri;
008 import android.os.Bundle;
009 import android.provider.MediaStore.Audio;
010 import android.util.Log;
011 import android.view.View;
012 import android.view.View.OnClickListener;
013 import android.widget.Button;
014 import android.widget.RemoteViews;
015 import android.widget.SeekBar;
016 import android.widget.TextView;
017 /**
018  * Notification
019  * @author  Administrator
020  *
021  */
022 public class Lesson_10 extends Activity {
023      
024     //BaseNotification
025     private Button bt01;
026      
027     //UpdateBaseNotification
028     private Button bt02;
029      
030     //ClearBaseNotification
031     private Button bt03;
032      
033     //MediaNotification
034     private Button bt04;
035      
036     //ClearMediaNotification
037     private Button bt05;
038      
039     //ClearALL
040     private Button bt06;
041      
042     //CustomNotification
043     private Button bt07;
044      
045     //通知管理器
046     private NotificationManager nm;
047      
048     //通知顯示內容
049     private PendingIntent pd;
050      
051     @Override
052      public void onCreate(Bundle savedInstanceState) {
053             super.onCreate(savedInstanceState);
054             /*加載頁面*/
055             setContentView(R.layout.lesson10);
056              
057             init();
058     }
059      
060     private void init() {
061         bt01 = (Button)findViewById(R.id.le10bt01);
062         bt02 = (Button)findViewById(R.id.le10bt02);
063         bt03 = (Button)findViewById(R.id.le10bt03);
064         bt04 = (Button)findViewById(R.id.le10bt04);
065         bt05 = (Button)findViewById(R.id.le10bt05);
066         bt06 = (Button)findViewById(R.id.le10bt06);
067         bt07 = (Button)findViewById(R.id.le10bt07);
068          
069         bt01.setOnClickListener(onclick);
070         bt02.setOnClickListener(onclick);
071         bt03.setOnClickListener(onclick);
072         bt04.setOnClickListener(onclick);
073         bt05.setOnClickListener(onclick);
074         bt06.setOnClickListener(onclick);  
075         bt07.setOnClickListener(onclick);
076          
077         nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
078          
079         Intent intent = new Intent(this,Lesson_10.class);
080          
081         pd = PendingIntent.getActivity(Lesson_10.this, 0, intent, 0);
082     }
083      
084     OnClickListener onclick = new OnClickListener() {
085          
086         //BASE Notification ID
087         private int Notification_ID_BASE = 110;
088          
089         private Notification baseNF;
090          
091         //Notification ID
092         private int Notification_ID_MEDIA = 119;
093          
094         private Notification mediaNF;
095          
096         @Override
097         public void onClick(View v) {
098             switch(v.getId()) {
099                 case R.id.le10bt01:
100                     //新建狀態欄通知
101                     baseNF = new Notification();
102                       
103                     //設置通知在狀態欄顯示的圖標
104                     baseNF.icon = R.drawable.icon;
105                      
106                     //通知時在狀態欄顯示的內容
107                     baseNF.tickerText = "You clicked BaseNF!";
108                      
109                     //通知的默認參數 DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS.
110                     //如果要全部采用默認值, 用 DEFAULT_ALL.
111                     //此處采用默認聲音
112                     baseNF.defaults |= Notification.DEFAULT_SOUND;
113                     baseNF.defaults |= Notification.DEFAULT_VIBRATE;
114                     baseNF.defaults |= Notification.DEFAULT_LIGHTS;
115                      
116                     //讓聲音、振動無限循環,直到用戶響應
117                     baseNF.flags |= Notification.FLAG_INSISTENT;
118                      
119                     //通知被點擊后,自動消失
120                     baseNF.flags |= Notification.FLAG_AUTO_CANCEL;
121                      
122                     //點擊'Clear'時,不清楚該通知(QQ的通知無法清除,就是用的這個)
123                     baseNF.flags |= Notification.FLAG_NO_CLEAR;
124                      
125                      
126                     //第二個參數 :下拉狀態欄時顯示的消息標題 expanded message title
127                     //第三個參數:下拉狀態欄時顯示的消息內容 expanded message text
128                     //第四個參數:點擊該通知時執行頁面跳轉
129                     baseNF.setLatestEventInfo(Lesson_10.this, "Title01", "Content01", pd);
130                      
131                     //發出狀態欄通知
132                     //The first parameter is the unique ID for the Notification
133                     // and the second is the Notification object.
134                     nm.notify(Notification_ID_BASE, baseNF);
135                      
136                     break;
137                      
138                 case R.id.le10bt02:
139                     //更新通知
140                     //比如狀態欄提示有一條新短信,還沒來得及查看,又來一條新短信的提示。
141                     //此時采用更新原來通知的方式比較。
142                     //(再重新發一個通知也可以,但是這樣會造成通知的混亂,而且顯示多個通知給用戶,對用戶也不友好)
143                     baseNF.setLatestEventInfo(Lesson_10.this, "Title02", "Content02", pd);
144                     nm.notify(Notification_ID_BASE, baseNF);
145                     break;
146                      
147                 case R.id.le10bt03:
148                      
149                     //清除 baseNF
150                     nm.cancel(Notification_ID_BASE);
151                     break;
152                      
153                 case R.id.le10bt04:
154                     mediaNF = new Notification();
155                     mediaNF.icon = R.drawable.icon;
156                     mediaNF.tickerText = "You clicked MediaNF!";
157                      
158                     //自定義聲音
159                     mediaNF.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
160                      
161                     //通知時發出的振動
162                     //第一個參數: 振動前等待的時間
163                     //第二個參數: 第一次振動的時長、以此類推
164                     long[] vir = {0,100,200,300};
165                     mediaNF.vibrate = vir;
166                      
167                     mediaNF.setLatestEventInfo(Lesson_10.this, "Title03", "Content03", pd);
168                      
169                     nm.notify(Notification_ID_MEDIA, mediaNF);
170                     break;
171                      
172                 case R.id.le10bt05:
173                     //清除 mediaNF
174                     nm.cancel(Notification_ID_MEDIA);
175                     break;
176                      
177                 case R.id.le10bt06:
178                     nm.cancelAll();
179                     break;
180                      
181                 case R.id.le10bt07:
182                     //自定義下拉視圖,比如下載軟件時,顯示的進度條。
183                     Notification notification = new Notification();
184                      
185                     notification.icon = R.drawable.icon;
186                     notification.tickerText = "Custom!";
187                      
188                     RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom);
189                     contentView.setImageViewResource(R.id.image, R.drawable.icon);
190                     contentView.setTextViewText(R.id.text, "Hello, this message is in a custom expanded view");
191                     notification.contentView = contentView;
192                      
193                     //使用自定義下拉視圖時,不需要再調用setLatestEventInfo()方法
194                     //但是必須定義 contentIntent
195                     notification.contentIntent = pd;
196                      
197                     nm.notify(3, notification);
198                     break;
199             }
200         }
201     };
202      
203      
204 }

主頁面:

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout
03   xmlns:android="http://schemas.android.com/apk/res/android"
04   android:layout_width="fill_parent"
05   android:layout_height="fill_parent"
06   android:orientation="vertical">
07     <Button
08         android:id="@+id/le10bt01"
09         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>

自定義視圖頁面:

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03               android:orientation="horizontal"
04               android:layout_width="fill_parent"
05               android:layout_height="fill_parent"
06               android:padding="3dp"
07               >
08     <ImageView android:id="@+id/image"
09               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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM