Android消息通知(notification)和PendingIntent傳值

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

NotificationManagerhtml


NotificationManager負責通知用戶事件的發生。

 NotificationManager有三個公共方法:android


1.    cancel(int id)    取消之前顯示的一個通知.假如是一個短暫的通知,試圖將隱藏,假如是一個持久的通知,將從狀態條中移走.
2.    cancelAll()    取消之前顯示的全部通知。
3.    notify(int id, Notification notification)     把通知持久的發送到狀態條上.
 NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Notification表明着一個通知.ide

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發送到狀態條上:ui

複製代碼

Notification notification =  Notification(R.drawable.icon, "Service started", System.currentTimeMillis());
 
PendingIntent contentIntent = PendingIntent.getActivity(, 0,  Intent(, Main.), 0);
 
  
notification.setLatestEventInfo(, "Test Service", "Service started", contentIntent);
 
nm.notify(R.string.hello, notification);

複製代碼

Notification的取消
nm.cancel(R.string.hello);
完整代碼實現

複製代碼

   addNotificaction(String pId,String pTtitle,String pContent) {
        NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
                Notification notification =  Notification();
                notification.icon = R.drawable.icon;
                notification.tickerText = pTtitle;  
              
          
                notification.defaults=Notification.DEFAULT_SOUND;  
                notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;  
          
                Intent intent =  Intent(, AndroidMain.);  
        PendingIntent pendingIntent = PendingIntent.getActivity(, 0, intent, PendingIntent.FLAG_ONE_SHOT);  
                notification.setLatestEventInfo(, pTtitle, pContent, pendingIntent);  
        manager.notify(id, notification);  
    }

複製代碼

Pendingintent傳值問題
pendingintent傳值常常獲取到的值是第一次的值或者null,這個跟第二個參數和最後一個參數選擇有關係。
PendingIntent pendingIntent = PendingIntent.getActivity(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

注:若是所要啓動的Activity是單例模式,其傳值方法請看onNewIntent調用時機this

總結一下pendingIntent的經常使用FLAG標籤spa

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.code

上面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值.orm

相關文章
相關標籤/搜索