android notification的使用

一、 Notification 簡介

 

在 android 系統中,在應用程序可能會遇到幾種情況需要通知用戶,有的需要用戶迴應,有的則不需要,例如:

* 當保存文件等事件完成,應該會出現一個小的消息,以確認保存成功。

* 如果應用程序在後臺運行,需要用戶的注意,應用程序應該創建一個通知,允許用戶在他或她的迴應提供便利

* 如果應用程序正在執行的工作,用戶必須等待(如裝載文件),應用程序應該顯示進度或等待提醒。

 

針對這些情況, android 都提供了不同的提醒方式。主要包括下面幾種:

1. Toast Notification 是指出現在屏幕上的暫時性通知,這種通知用於傳達一些告知類型的消息,短暫停留後會自動消失,無需用戶交互。比如告知下載已完成等。 (Toast Noification 這個說法最早是源於一個前 MSN 員工的提法, 因爲 MSN 的消息提醒是從底部向上輕彈,形式上很像一個麪包從烤麪包機中彈起的樣子,所以稱之爲 Toast Noification 。 )

2. Status Bar Notification 是指以一個圖標或者滾動條文本的形式出現在系統頂部狀態欄上的通知。當應用程序處於後臺運行狀態時,這種方式比較合適。這種通知形式的好處是既能即使被關注到,又無需打斷當前任務,可以從頂部下拉查看通知摘並做選擇性處理。

3. Dialog Notification 類似於 iOS 的 Alert Notification ,以對話窗口的形式出現在屏幕上,用於重要或需及時處理的通知。

 

下面我們先了解以下 Android notification 的整個架構。前二種提醒方式都是由 NotificationManagerService ,而 Dialog Notification ,則是彈出一個窗口形 式實現的,因爲這種提醒方式大多是針對當前應用程序或進程,所以它只是一種簡單且直觀的表達方式。



 

二、 Notification的使用


1.Toast
Toast 是 Android 中用來顯示顯示信息的一種機制,和 Dialog 不一樣的是, Toast 是沒有焦點的,而且 Toast 顯示的時間有限,過一定的時間就會自動消失

Java代碼  收藏代碼
  1. // 使用 TOAST 方法顯示結果內容   
  2. Toast textToast=Toast.makeText(this" 提示內容 ", Toast.LENGTH_LONG);   
  3.   
  4. //... 這裏也可以對 Toast 添加一些屬性   
  5. textToast.show();   

 

2. StatusBar Notification

StatusBar Notification 是在系統狀態欄上 增加了一個狀態欄圖標,並在“通知“窗口中顯示提示信息。當用戶選擇展開郵件, Android 就會發送一個通知(通常是推出一個活動)定義的意向。您也可以配置通知,提醒和聲音,震動的用戶,並在設備上閃爍的燈光。

這樣的通知是很理想的工作時,您的應用程序在後臺服務,需要通知有關事件的用戶。如果您需要提醒有關事件已經發生,而你的活動仍可以在當前焦點,此時可以考慮使用一個對話框通知代替。

StatusBar Notification 基本步驟如下:

1 )得到 NotificationManager :

Java代碼  收藏代碼
  1. String ns = Context.NOTIFICATION_SERVICE;   
  2.   
  3. NotificationManager mNotificationManager = (NotificationManager) getSystemService( ns );   

 2 )創建一個新的 Notification 對象:

Java代碼  收藏代碼
  1. Notification notification = new Notification();   
  2.   
  3. notification.icon = R.drawable.notification_icon;   

    也可以使用稍微複雜一些的方式創建 Notification :

Java代碼  收藏代碼
  1. int icon = R.drawable.notification_icon; //通知圖標   
  2.   
  3. CharSequence tickerText = "Hello"// 狀態欄 (Status Bar) 顯示的通知文本提示   
  4.   
  5. long when = System.currentTimeMillis(); // 通知產生的時間,會在通知信息裏顯示   
  6.   
  7. Notification notification = new Notification(icon, tickerText, when) ;  

  3 )填充 Notification 的各個屬性:

Java代碼  收藏代碼
  1. Context context = getApplicationContext();   
  2.   
  3. CharSequence contentTitle = "My notification";   
  4.   
  5. CharSequence contentText = "Hello World!";   
  6.   
  7. Intent notificationIntent = new Intent(this, MyClass.class);   
  8.   
  9. PendingIntent contentIntent = PendingIntent.getActivity(this0,notificationIntent, 0);   
  10.   
  11. notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);   

 

Notification 提供了豐富的手機提示方式:

a) 在狀態欄 (Status Bar) 顯示的通知文本提示,如:

Java代碼  收藏代碼
  1. notification.tickerText = "hello";   

 

b) 發出提示音,如:

Java代碼  收藏代碼
  1. notification.defaults |= Notification.DEFAULT_SOUND;   
  2.   
  3. notification.sound = Uri.parse("file:/ sdcard /notification/ringer.mp3");   
  4.   
  5. notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");   

 

c) 手機振動,如:

Java代碼  收藏代碼
  1. notification.defaults |= Notification.DEFAULT_VIBRATE;   
  2.   
  3. long[] vibrate = {0,100,200,300};   
  4.   
  5. notification.vibrate = vibrate ;  

 

d)LED 燈閃爍,如:

Java代碼  收藏代碼
  1. notification.defaults |= Notification.DEFAULT_LIGHTS;   
  2.   
  3. notification.ledARGB = 0xff00ff00;   
  4.   
  5. notification.ledOnMS = 300;   
  6.   
  7. notification.ledOffMS = 1000;   
  8.   
  9. notification.flags |= Notification.FLAG_SHOW_LIGHTS;   

 

e) 添加 remote view

通過 RemoteViews 設置 notification 中 View 的屬性

Java代碼  收藏代碼
  1. notification.contentView = new RemoteViews(getApplication().getPackageName(), R.layout.custom_dialog);   
  2.   
  3. notification.contentView.setProgressBar(R.id.pb, 1000false);   
  4.   
  5. notification.contentView.setTextViewText(R.id.tv, " 進度 " + _progress+ "%");   

 

4 )發送通知:

Java代碼  收藏代碼
  1. private static final int ID_NOTIFICATION = 1;   
  2.   
  3. mNotificationManager.notify(ID_NOTIFICATION, notification);   

 

 

3.Dialog Notification

3.1 AlertDialog

爲了創建一個警告對話框,使用 AlertDialog.Builder 子類。通過 AlertDialog.Builder

(Context) 獲取一個構造器然後使用這個類的公共方法來定義警告對話框的所有屬性。當得到構造器後,通過 create(). 方法來獲取警告對話框對象。有時我是不調用 create() 的,而是在設置好了後直接調用 show() 顯示 AlertDialog 。

Java代碼  收藏代碼
  1. AlertDialog.Builder builder=newAlertDialog.Builder(this);   
  2.   
  3. builder.setMessage("Areyousureyouwanttoexit?") ;   
  4.   
  5. AlertDialog alert=builder.create();   

 

3.2 ProcessDialog

ProgressDialog 是 AlertDialog 類的一個擴展,可以爲一個未定義進度的任務顯示一個旋轉輪形狀的進度動畫,或者爲一個指定進度的任務顯示一個進度條。

Java代碼  收藏代碼
  1. ProgressDialog progressDialog=newProgressDialog(getApplicationContext());   
  2.   
  3. progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);   
  4.   
  5. progressDialog.setIcon(R.drawable.alert_dialog_icon);   
  6.   
  7. progressDialog.setMessage("Loading...");   
  8.   
  9. progressDialog.setCancelable(false);   

 

原文地址: http://blog.csdn.net/ericyelai/article/details/6213046

發佈了1 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章