Android 之 Notification
當用戶有沒有接到的電話的時候,Android頂部狀態欄裏就會出現一個小圖標。提示用戶有沒有處理的快訊,當拖動狀態欄時,可以查看這些快訊。Android給我們提供了NotificationManager來管理這個狀態欄。可以很輕鬆的完成。
如果要添加一個Notification,可以按照以下幾個步驟
1:獲取NotificationManager:
NotificationManager m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
2:定義一個Notification:
Notification m_Notification=new Notification();
3:設置Notification的各種屬性:
//設置通知在狀態欄顯示的圖標
m_Notification.icon=R.drawable.icon;
//當我們點擊通知時顯示的內容
m_Notification.tickerText="Button1 通知內容.....";
通知時發出的默認聲音
m_Notification.defaults=Notification.DEFAULT_SOUND;
//設置通知顯示的參數
Intent m_Intent=new Intent(NotificationDemo.this,DesActivity.class);
PendingIntent m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);
m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1", "Button1通知",m_PendingIntent );
//這個可以理解為開始執行這個通知
m_NotificationManager.notify(0,m_Notification);
4:既然可以增加同樣我們也可以刪除。當然是隻是刪除你自己增加的。
m_NotificationManager.cancel(0);
這裏的0是一個ID號碼,和notify第一個參數0一樣。
這也就完成了,添加刪除工作。
這裏我們還是一個Demo來掩飾我們的操作。
1:新建一個工程NotificationDemo。
2:添加一個Activity:DesActivity,注意需要在Manifest.xml中聲明
3:NotificationDemo中的Laytout文件很簡單就是定義一個Button.其代碼文件如下:
- package com.rocky.studio.ch4221;
- import android.app.Activity;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class NotificationDemo extends Activity {
- Button m_Button1;
- TextView m_txtView;
- NotificationManager m_NotificationManager;
- Notification m_Notification;
- Intent m_Intent;
- PendingIntent m_PendingIntent;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
- m_Button1=(Button)this.findViewById(R.id.Button01);
- //點擊通知時轉移內容
- m_Intent=new Intent(NotificationDemo.this,DesActivity.class);
- m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);
- m_Notification=new Notification();
- m_Button1.setOnClickListener(new Button.OnClickListener(){
- public void onClick(View v) {
- // TODO Auto-generated method stub
- //設置通知在狀態欄顯示的圖標
- m_Notification.icon=R.drawable.icon;
- //當我們點擊通知時顯示的內容
- m_Notification.tickerText="Button1 通知內容.....";
- //通知時發出的默認聲音
- m_Notification.defaults=Notification.DEFAULT_SOUND;
- //設置通知顯示的參數
- m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1", "Button1通知",m_PendingIntent );
- //這個可以理解為開始執行這個通知
- m_NotificationManager.notify(0,m_Notification);
- }});
- }
- }
package com.rocky.studio.ch4221; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class NotificationDemo extends Activity { Button m_Button1; TextView m_txtView; NotificationManager m_NotificationManager; Notification m_Notification; Intent m_Intent; PendingIntent m_PendingIntent; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE); m_Button1=(Button)this.findViewById(R.id.Button01); //點擊通知時轉移內容 m_Intent=new Intent(NotificationDemo.this,DesActivity.class); m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0); m_Notification=new Notification(); m_Button1.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub //設置通知在狀態欄顯示的圖標 m_Notification.icon=R.drawable.icon; //當我們點擊通知時顯示的內容 m_Notification.tickerText="Button1 通知內容....."; //通知時發出的默認聲音 m_Notification.defaults=Notification.DEFAULT_SOUND; //設置通知顯示的參數 m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1", "Button1通知",m_PendingIntent ); //這個可以理解為開始執行這個通知 m_NotificationManager.notify(0,m_Notification); }}); } }
4:修改DesActivity 的源文件,代碼如下:它做的事情就是取消之前添加的Notification
- package com.rocky.studio.ch4221;
- import android.app.Activity;
- import android.app.NotificationManager;
- import android.os.Bundle;
- public class DesActivity extends Activity {
- NotificationManager m_NotificationManager;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.main2);
- //啟動後刪除之前我們定義的
- m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
- m_NotificationManager.cancel(0);
- }
- }
package com.rocky.studio.ch4221; import android.app.Activity; import android.app.NotificationManager; import android.os.Bundle; public class DesActivity extends Activity { NotificationManager m_NotificationManager; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.main2); //啟動後刪除之前我們定義的 m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE); m_NotificationManager.cancel(0); } }
代碼也很簡單。可以查看Notification , NotificationMananger 這兩個類來學習前後左右。
下麵是一篇文章,對Notification ,NotificationManager這兩個類有詳細的說明介紹,特借鑒一下。
NoticificationManager很容易可以放在狀態欄,也很容易實現從statusbar進入程序 中,
NoticificationManager中通過intent執行此程序的activity就可以了
NoticificationManager狀態欄操作
NotificationManager(通知管理器):
NotificationManager負責通知用戶事件的發生.
NotificationManager有三個公共方法:
1. cancel(int id) 取消以前顯示的一個通知.假如是一個短暫的通知,試圖將隱藏,假如是一個持久的通知,將從狀態條中移走.
2. cancelAll() 取消以前顯示的所有通知.
3. notify(int id, Notification notification) 把通知持久的發送到狀態條上.
//初始化NotificationManager:
NotificationManager nm =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification代表著一個通知.
Notification的屬性:
audioStreamType 當聲音響起時,所用的音頻流的類型
contentIntent 當通知條目被點擊,就執行這個被設置的Intent.
contentView 當通知被顯示在狀態條上的時候,同時這個被設置的視圖被顯示.
defaults 指定哪個值要被設置成默認的.
deleteIntent 當用戶點擊"Clear All Notifications"按鈕區刪除所有的通知的時候,這個被設置的Intent被執行.
icon 狀態條所用的圖片.
iconLevel 假如狀態條的圖片有幾個級別,就設置這裏.
ledARGB LED燈的顏色.
ledOffMS LED關閉時的閃光時間(以毫秒計算)
ledOnMS LED開始時的閃光時間(以毫秒計算)
number 這個通知代表事件的號碼
sound 通知的聲音
tickerText 通知被顯示在狀態條時,所顯示的信息
vibrate 振動模式.
when 通知的時間戳.
將Notification發送到狀態條上:
Notification notification = new Notification();
Notification的設置過程……..
nm.notify(0, notification); //發送到狀態條上
創建和觸發Notification
創建和配置新的Notification需要經曆三步。
首先,你要創建一個新的Notification對象,傳入要在狀態條上顯示的圖 標、文字和Notification的 當前時間,如下麵的代碼片段所示:
// Choose a drawable to display as the status bar icon
int icon = R.drawable.icon;
// Text to display in the status bar when the notification is launched
String tickerText = “Notification”;
// The extended status bar orders notification in time order
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
當Notification觸發時,文本將沿著狀態條進行滾動 顯示。
其次,使用setLatestEventInfo方法來配置Notification在擴展的狀態窗口中的外觀。擴展的狀態窗口將顯示圖標和在構造函數中傳入的時間,以及顯示標題和一個詳細的字符串。Notification一般表示為對一個動作的請求或引起用戶的注意,所以,當用戶點擊Notification項目時你可以指定一個PendingIntent來觸發。
下麵的代碼片段使用了setLastestEventInfo來設置這些值:
Context context = getApplicationContext();
// Text to display in the extended status window
String expandedText = “Extended status text”;
// Title for the expanded status
String expandedTitle = “Notification Title”;
// Intent to launch an activity when the extended text is clicked
Intent intent = new Intent(this, MyActivity.class);
PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context,
expandedTitle,
expandedText,
launchIntent);
一個好的形式是顯示相同事件(例如,接 收多個SMS消息)的多個對象時 使用一個Notification圖 標。為了呈現給用戶,使用setLastestEventInfo更新數據集來呈現最近的消息以及重新觸發Notification來更新它的值。
你還可以使用number屬性來顯示一個狀態條圖標所表示的事件數目。
設置為比1大的數,如下所示,將在狀態條圖標上以一個小小的數字進行 顯示:
notification.number++;
任何對Notification的變更,你都需要重新觸發來進行更 新。如果要刪除這個數字,設置number的值為0或者-1。
最後,你可以對Notification對象使用多種屬性來增強Notification的效果,如閃爍LED、震動電話和播放音樂文件。這些高級特征將在本章的後麵部分進行詳細描述。
觸發Notification
為了觸發一個Notification,使用NotificationManager的notify方法,將一個整數的ID和Notification對象傳入,如下的片段所示:
int notificationRef = 1;
notificationManager.notify(notificationRef, notification);
為了更新一個已經觸發過的Notification,傳入相同的ID。你既可以傳入相同的Notification對象,也可以是一個全新的對象。隻 要ID相同,新的Notification對象會替換狀態條
圖標和擴展的狀態窗口的細節。
你還可以使用ID來取消Notification,通過調用NotificationManager的cancel方法,如下所示:
notificationManager.cancel(notificationRef);
取消一個Notification時,將移除它的狀態條圖標以及清除 在擴展的狀態窗口中的信息。
Notification和NotificationManager的基本使用方法
1. NotificationManager和Notification用來設置通知。
通知的設置等操作相對比較簡單,基本的使用方式就是用新建一個Notification對象,然後設置好通知的各項參數,然後使用係統後台運行的 NotificationManager服務將通知發出來。
基本步驟如下:
1)得到NotificationManager:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
2)創建一個新的Notification對象:
Notification notification = new Notification();
notification.icon = R.drawable.notification_icon;
也可以使用稍微複雜一些的方式創建Notification:
int icon = R.drawable.notification_icon; //通知圖標
CharSequence tickerText = "Hello"; //狀態欄(Status Bar)顯示的通知文本提示
long when = System.currentTimeMillis(); //通知產生的時間,會在通知信息裏顯示
Notification notification = new Notification(icon, tickerText, when);
3)填充Notification的各個屬性:
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
Notification提供了豐富的手機提示方式:
a)在狀態欄(Status Bar)顯示的通知文本提示,如:
notification.tickerText = "hello";
b)發出提示音,如:
notification.defaults |= Notification.DEFAULT_SOUND;
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
c)手機振動,如:
notification.defaults |= Notification.DEFAULT_VIBRATE;
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
d)LED燈閃爍,如:
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
4)發送通知:
private static final int ID_NOTIFICATION = 1;
mNotificationManager.notify(ID_NOTIFICATION, notification);
2. 通知的更新
如果需要更新一個通知,隻需要在設置好notification之後,再調用setLatestEventInfo,然後重新發送一次通知即可。
最後更新:2017-04-02 06:51:50