Android支持Toast和NotificationManager兩種通知方式,前者相當於一個定時關閉的對話框,后者是在狀態欄上顯示一條消息。Toast和Notification都可以隨時取消。
Toast
A toast is a view containing a quick little message for the user. The toast class helps you create and show those. Toast的使用很簡單:
Toast.makeText(this, "Service destroyed…", Toast.LENGTH_LONG).show();
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的公共方法:
describeContents() Describe the kinds of special objects contained in this Parcelable's marshalled representation.
setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) 設置Notification留言條的參數
writeToParcel(Parcel parcel, int flags) Flatten this notification from a parcel.
toString() …………….
將Notification發送到狀態條上:
Notification notification = new Notification(R.drawable.icon, "Service started", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Main.class), 0);
// must set this for content view, or will throw a exception
notification.setLatestEventInfo(this, "Test Service", "Service started", contentIntent);
nm.notify(R.string.hello, notification);
Notification的取消
nm.cancel(R.string.hello);
完整代碼實現
public static void addNotificaction(String pId,String pTtitle,String pContent) {
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// 創建一個Notification
Notification notification = new Notification();
// 設置顯示在手機最上邊的狀態欄的圖標
notification.icon = R.drawable.icon;
// 當當前的notification被放到狀態欄上的時候,提示內容
notification.tickerText = pTtitle;
/***
* notification.contentIntent:一個PendingIntent對象,當用戶點擊了狀態欄上的圖標時,該Intent會被觸發
* notification.contentView:我們可以不在狀態欄放圖標而是放一個view
* notification.deleteIntent 當當前notification被移除時執行的intent
* notification.vibrate 當手機震動時,震動周期設置
*/
// 添加聲音提示
notification.defaults=Notification.DEFAULT_SOUND;
// audioStreamType的值必須AudioManager中的值,代表着響鈴的模式
notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;
//下邊的兩個方式可以添加音樂
//notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
//notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
Intent intent = new Intent(this, AndroidMain.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
// 點擊狀態欄的圖標出現的提示信息設置
notification.setLatestEventInfo(this, pTtitle, pContent, pendingIntent);
manager.notify(id, notification);
}
Pendingintent傳值問題
pendingintent傳值經常獲取到的值是第一次的值或者null,這個跟第二個參數和最后一個參數選擇有關系。
PendingIntent pendingIntent = PendingIntent.getActivity(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
注:如果所要啟動的Activity是單例模式,其傳值方法請看onNewIntent調用時機
總結一下pendingIntent的常用FLAG標簽:
FLAG_ONE_SHOT:this PendingIntent can only be used once. If set, after send() is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.
FLAG_NO_CREATE:if the described PendingIntent does not already exist, then simply return null instead of creating it.
FLAG_CANCEL_CURRENT:if the described PendingIntent already exists, the current one is canceled before generating a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAG_UPDATE_CURRENT.
FLAG_UPDATE_CURRENT: if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
上面4個flag中最經常使用的是FLAG_UPDATE_CURRENT,因為描述的Intent有更新的時候需要用到這個flag去更新你的描述,否則組件在下次事件發生或時間到達的時候extras永遠是第一次Intent的extras。使用FLAG_CANCEL_CURRENT也能做到更新extras,只不過是先把前面的extras清除,另外FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT的區別在於能否新new一個Intent,FLAG_UPDATE_CURRENT能夠新new一個Intent,而FLAG_CANCEL_CURRENT則不能,只能使用第一次的Intent。
另外兩flag就比較少用,利用FLAG_ONE_SHOT獲取的PendingIntent只能使用一次,再使用PendingIntent也將失敗,利用FLAG_NO_CREAT獲取的PendingIntent若描述的Intent不存在則返回NULL值.
