1. 程式人生 > >Android NOtification 使用(震動 閃屏 鈴聲)

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);