移動測試之Notification

一、Notification:
(一)、簡介:
        顯示在手機狀態欄的通知。Notification所代表的是一種具有全局效果的通知,程序一般通過NotificationManager服務來發送Notification。
        Android3.0增加了Notification.Builder類,該類可以輕鬆地創建Notification對象。

Notification,俗稱通知,是一種具有全局效果的通知,它展示在屏幕的頂端,首先會表現爲一個圖標的形式,當用戶向下滑動的時候,展示出通知具體的內容。

  【注意:】因爲一些Android版本的兼容性問題,對於Notification而言,Android3.0是一個分水嶺,在其之前構建Notification推薦使用Notification.Builder構建,而在Android3.0之後,一般推薦使用NotificationCompat.Builder構建。

通知一般通過NotificationManager服務來發送一個Notification對象來完成,NotificationManager是一個重要的系統級服務,該對象位於應用程序的框架層中,應用程序可以通過它像系統發送全局的通知。這個時候需要創建一個Notification對象,用於承載通知的內容。但是一般在實際使用過程中,一般不會直接構建Notification對象,而是使用它的一個內部類NotificationCompat.Builder來實例化一個對象(Android3.0之下使用Notification.Builder),並設置通知的各種屬性,最後通過NotificationCompat.Builder.build()方法得到一個Notification對象。當獲得這個對象之後,可以使用NotificationManager.notify()方法發送通知。

  NotificationManager類是一個通知管理器類,這個對象是由系統維護的服務,是以單例模式獲得,所以一般並不直接實例化這個對象。在Activity中,可以使用Activity.getSystemService(String)方法獲取NotificationManager對象,Activity.getSystemService(String)方法可以通過Android系統級服務的句柄,返回對應的對象。在這裏需要返回NotificationManager,所以直接傳遞Context.NOTIFICATION_SERVICE即可。

  雖然通知中提供了各種屬性的設置,但是一個通知對象,有幾個屬性是必須要設置的,其他的屬性均是可選的,必須設置的屬性如下:

小圖標,使用setSamllIcon()方法設置。
標題,使用setContentTitle()方法設置。
文本內容,使用setContentText()方法設置。 

(二)、Notification.Builder類中提供的方法:
builder.setAutoCancel(); 設置點擊通知後,狀態欄自動刪除通知。
builder.setSmallIcon(R.drawable.alert); 設置通知小圖標
builder.setLargeIcon(R.drawable.alert2); 設置通知大圖標
builder.setContentTitle("標題"); 設置通知標題
builder.setContentText("文本");  設置通知內容
builder.setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE);  設置通知的音樂、振動、LED等。
builder.setSound();  設置通知的音樂
builder.setTicker();  設置通知在狀態欄的提示文本。第一次提示消息的時候顯示在通知欄上。
builder.setContentIntent();  設置點擊通知後將要啓動的程序組件對應的PendingIntent

(三)、發送Notification的步驟:(四部曲)
1、調用getSystemService(NOTIFICATION_SERVICE)方法獲取系統的NotificationManager服務,它是一個重要的系統服務。應用程序可以通過NotificationManager 向系統發送全局通知;
2、構造Notification.Builder對象;
3、設置Notification.Builder對象的各種屬性;
4、通過NotificationManager 的notify()方法發送Notification。

(四)、示例代碼:
核心代碼如下:
public void clickButton(View view) {
switch (view.getId()) {
case R.id.button_main_common:
Notification.Builder builder1 = new Notification.Builder(this);
builder1.setSmallIcon(R.drawable.ic_launcher);
builder1.setContentTitle("提示:");
builder1.setContentText("請注意休息,時間到了!"); builder1.setAutoCancel(true);

Intent intent = new Intent(this, NextActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent,
PendingIntent.FLAG_ONE_SHOT);
builder1.setContentIntent(pIntent);

nManager.notify(R.id.button_main_common, builder1.build());
break;
case R.id.button_main_bigpicture:
Notification.Builder builder2 = new Notification.Builder(this);
builder2.setSmallIcon(R.drawable.alert);
builder2.setContentTitle("提示:");
builder2.setContentText("以下是展示的大圖片。。。");

Notification.BigPictureStyle bigStyle = new BigPictureStyle(builder2);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.big);
bigStyle.bigPicture(bitmap);
nManager.notify(R.id.button_main_bigpicture, bigStyle.build());

break;
case R.id.button_main_progress:
final Notification.Builder builder3 = new Notification.Builder(this);
builder3.setSmallIcon(R.drawable.alert);
builder3.setContentTitle("提示:");
builder3.setContentText("數據下載中。。。");

new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 100; i += 5) {
builder3.setProgress(100, i, false);
nManager.notify(R.id.button_main_progress,
builder3.build());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
builder3.setContentText("下載完畢!");
nManager.notify(R.id.button_main_progress, builder3.build()); nManager.cancel(R.id.button_main_progress);
}
}).start();

break;
case R.id.button_main_clear:
nManager.cancelAll();
break;
}
}

(五)、PendingIntent:
1、PendingIntent字面意義:等待的,懸而未決的Intent;Intent一般是用作Activity、Sercvice、BroadcastReceiver之間傳遞數據,而Pendingintent,一般用在 Notification上,可以理解爲延遲執行的intent,PendingIntent是對Intent一個包裝;
2、得到一個 PendingIntent 對象,使用方法類的靜態方法 getActivity(Context, int, Intent, int);
3、PendingIntent是一種特殊的Intent。主要的區別在於Intent是立刻執行,而 PendingIntent 的執行不是立刻,而是當條件滿足後才發送企圖,而且PendingIntent 可以取消;
4、PendingIntent執行的操作實質上是參數傳進來的Intent的操作,使用 PendingIntent 的目的在於它所包含的Intent的操作的執行是需要滿足某些條件的。
5、主要的使用的地方和例子:通知Notificatio的發送,短消息SmsManager的發送和警報器AlarmManager的執行等。
        總而言之,PendingIntent就是一個可以在滿足一定條件下執行的Intent,它相比於Intent的優勢在於自己攜帶有Context對象,這樣他就不必依賴於某個activity纔可以存在。 

(六)、Intent和PendingIntent的區別:【掌握,以備面試之需】
1. Intent是立即使用的,而PendingIntent可以等到事件發生後觸發,PendingIntent可以cancel;
2. Intent在程序結束後即終止,而PendingIntent在程序結束後依然有效;
3. PendingIntent自帶Context,而Intent需要在某個Context內運行;
4. Intent在原task中運行,PendingIntent在新的task中運行。
(七)、PendingIntent的幾個常量:(getActivity(Context, int, Intent, int)方法中的第四個參數)
1.FLAG_ONE_SHOT  : 這個PendingIntent只能使用一次。
2.FLAG_NO_CREATE : 如果被描述的PendingIntent不存在,那麼簡單地返回null,而不是創建它。
3.FLAG_CANCEL_CURRENT  :  如果被描述的PendingIntent已經存在,在即將生成一個新的PendingIntent前,當前的一個要被取消。
FLAG_UPDATE_CURRENT  :如果被描述的PendingIntent已經存在,那麼繼續保持它,但它其中的數據會因爲新Intent而更新。

編輯:千鋒軟件測試

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章