日韩性视频-久久久蜜桃-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传值的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 婷婷综合在线观看 | www.成人网.com | 免费看91| 国产极品福利 | 蜜桃成熟时李丽珍国语 | 欧美xx在线| 日本激情在线 | 日本公与丰满熄 | 精品视频久久 | 欧美狂猛xxxxx乱大交3 | 亚洲天堂2024 | 国产久视频 | av电影免费在线播放 | 日韩网站在线 | 国产swag在线观看 | 五月色婷婷综合 | 国产盗摄一区二区三区在线 | 成人免费不卡视频 | 亚洲AV成人无码电影在线观看 | avwww.| 手机成人免费视频 | 色人阁婷婷 | 日韩经典一区二区三区 | 91亚洲精华国产精华精华液 | 免费中文字幕av | 亚洲少妇xxx | 91丝袜呻吟高潮美腿白嫩 | 两性视频久久 | 欧美第一页浮力影院 | 国产制服91一区二区三区制服 | 福利视频在线导航 | av爽妇网 | 24小时日本在线www免费的 | 美女丝袜合集 | 精品无码av一区二区三区 | 中文字幕电影一区 | v在线 | 成人3d动漫一区二区三区 | 国产精品一区在线免费观看 | 国产无码精品一区二区 | 影音先锋中文字幕在线播放 | 国产精品第二页 | 手机成人在线视频 | 久久久久久久久久久久久久久久久 | 香蕉一区二区 | 欧美精品v国产精品v日韩精品 | 一级做a爱 | 天天狠狠| 一区二区视频免费在线观看 | 黄色av国产 | 日日天天干 | 午夜一区二区三区在线观看 | 国产20页| 亚洲欧美视频在线播放 | 特黄特色大片免费 | www.第四色 | 亚洲一区国产 | 精品亚洲成人 | 欧美区一区二区 | 狠狠躁日日躁夜夜躁 | 国产喷水吹潮视频www | 久操不卡 | 日韩色| 中文字幕人成乱码熟女香港 | 亚洲男人皇宫 | 成年人在线网站 | 99精品视频播放 | 夜夜精品一区二区无码 | 亚洲插| aa一级黄色片 | 95看片淫黄大片一级 | 麻豆av免费在线观看 | 欧美性受xxxx黑人猛交88 | 国产不卡视频在线播放 | 可以直接看的无码av | 日韩黄色一级片 | 无码少妇一区二区三区芒果 | 搡老熟女国产 | 亚洲一区不卡在线 | 手机在线成人av | 久久人人爽人人爽 | 亚洲在线综合 | 桃色在线视频 | 亚洲另类在线观看 | 欧美变态口味重另类 | 波多野吉衣一二三区乱码 | 日本电影大尺度免费观看 | 高潮疯狂过瘾粗话对白 | 97麻豆| 伊人久久婷婷 | 欧美日韩国产精品一区二区三区 | 日韩黄 | 亚洲熟妇色自偷自拍另类 | 激情成人综合 | 亚洲欧美一区二区三区四区 | 色悠悠国产 | 日本在线高清视频 | 在线观看 中文字幕 | 成人激情视频在线 |