android 通知传值,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();
NotificationManager
NotificationManager負責通知用戶事件的發生。
NotificationManager有三個公共方法:
1.????cancel(int?id)????取消以前顯示的一個通知.假如是一個短暫的通知,試圖將隱藏,假如是一個持久的通知,將從狀態條中移走.
2.????cancelAll()????取消以前顯示的所有通知。
3.????notify(int?id,?Notification?notification)?????把通知持久的發送到狀態條上.
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?=??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調用時機
總結一下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值.
總結
以上是生活随笔為你收集整理的android 通知传值,Android消息通知(notification)和PendingIntent传值的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: zenmap nmap输出无显示_双路显
 - 下一篇: 域控服务器取消验证_记一次域控服务器应急