日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 通知传值,Android消息通知(notification)和PendingIntent传值

發布時間:2025/3/12 Android 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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传值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 四虎久久久久 | 欧美图片自拍偷拍 | 九九热这里只有精品6 | 久久久久久久黄色片 | 草av在线 | 69堂在线观看 | 国产午夜精品一区二区理论影院 | 亚洲色图视频在线观看 | 欧美成a | 欧美人狂配大交3d | 亚欧美视频 | www.四虎在线| 黄色一级影片 | 亚洲永久免费 | 日本免费高清视频 | 西比尔在线观看完整视频高清 | 国产午夜大片 | 国产va亚洲va在线va | 日韩女同一区二区三区 | 国产视频你懂的 | 成人激情开心 | 少妇精品一区二区 | 男人天堂av在线播放 | 久久中文字幕人妻 | 艳妇臀荡乳欲伦交换在线播放 | 欧美熟妇交换久久久久久分类 | 免费观看黄色网 | 中文字幕精品在线视频 | 日韩视频一二三区 | 草草视频在线观看 | 丰满岳跪趴高撅肥臀尤物在线观看 | 性欧美一级 | 韩国日本美国免费毛片 | 内射干少妇亚洲69xxx | 国产精品国产三级国产在线观看 | 成年人晚上看的视频 | 亚洲成人一区 | 日本在线观看一区二区三区 | 国产一级二级在线 | 免费日本黄色网址 | 精品视频在线观看一区二区 | 朝桐光在线视频 | 日韩高清影视在线观看 | 天天摸天天 | jizz性欧美2 视频在线日韩 | 亚洲人成人一区二区在线观看 | 99精品视频一区二区 | 99国产精品免费视频 | 欧美日韩一区二区三区在线电影 | 国产三级精品在线观看 | 色大师在线观看 | 毛茸茸成熟亚洲人 | 中文字幕一区二 | 操操网站| 久热久| 午夜xxxx| 偷拍综合网 | 国产三级在线观看 | 不卡成人| 国产又粗又黄又爽又硬的视频 | 在线免费观看污网站 | 国产波霸爆乳一区二区 | 日韩片在线观看 | 公侵犯人妻一区二区三区 | 成人爱爱网站 | 好色艳妇小说 | 亚洲黄色短视频 | 久操视频免费看 | 国产精品天干天干 | 亚洲第一成人在线 | 白又丰满大屁股bbbbb | 91美女啪啪 | 日本不卡影院 | 国产又粗又黄又爽 | 激情亚洲视频 | 中国少妇高潮 | 97超碰国产在线 | 日韩国产欧美一区二区 | 国产精品九九热 | 亚洲免费福利 | 一区二区一级片 | 麻豆一区二区在线 | 在线天堂中文 | 美女隐私免费看 | 美女搞黄在线观看 | 麻豆porn| 老汉av在线 | 国产456| 亚洲品质自拍视频网站 | 爱爱小视频免费看 | 国产精品手机在线观看 | 四十路av| 日韩在线91| 无套内谢少妇露脸 | 亚洲啊v | 亚洲卡一 | 精品一区二区在线看 | www.中文字幕在线观看 | 欧美国产精品一二三 |