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 顯示的時間有限,過一定的時間就會自動消失 

// 使用 TOAST 方法顯示結果內容 
Toast textToast=Toast.makeText(this, " 提示內容 ", Toast.LENGTH_LONG); 
//... 這裏也可以對 Toast 添加一些屬性 
textToast.show(); 
2. StatusBar Notification 
StatusBar Notification 是在系統狀態欄上 增加了一個狀態欄圖標,並在“通知“窗口中顯示提示信息。當用戶選擇展開郵件, Android 就會發送一個通知(通常是推出一個活動)定義的意向。您也可以配置通知,提醒和聲音,震動的用戶,並在設備上閃爍的燈光。 
這樣的通知是很理想的工作時,您的應用程序在後臺服務,需要通知有關事件的用戶。如果您需要提醒有關事件已經發生,而你的活動仍可以在當前焦點,此時可以考慮使用一個對話框通知代替。 

StatusBar Notification 基本步驟如下: 

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; 

e) 添加 remote view 
通過 RemoteViews 設置 notification 中 View 的屬性 
notification.contentView = new RemoteViews(getApplication().getPackageName(), R.layout.custom_dialog); 
notification.contentView.setProgressBar(R.id.pb, 100, 0, false); 
notification.contentView.setTextViewText(R.id.tv, " 進度 " + _progress+ "%"); 

4 )發送通知: 
private static final int ID_NOTIFICATION = 1; 
mNotificationManager.notify(ID_NOTIFICATION, notification); 


3.Dialog Notification

3.1 AlertDialog 

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

AlertDialog.Builder builder=newAlertDialog.Builder(this); 
builder.setMessage("Areyousureyouwanttoexit?") ; 
AlertDialog alert=builder.create(); 

3.2 ProcessDialog 

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

ProgressDialog progressDialog=newProgressDialog(getApplicationContext()); 
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
progressDialog.setIcon(R.drawable.alert_dialog_icon); 
progressDialog.setMessage("Loading..."); 

progressDialog.setCancelable(false); 


轉自:http://blog.csdn.net/ge_zhiqiang/article/details/6748385

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