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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?

發布時間:2024/7/19 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

情況是這樣的,使用NotificationManager觸發多個Notification:

Java代碼 ?
  • private?Notification?genreNotification(Context?context,?int?icon,?String?tickerText,?String?title,?String?content,?Intent?intent){ ??
  • ????????Notification?notification?=?new?Notification(icon,?tickerText,?System.currentTimeMillis()); ??
  • ????????PendingIntent?pendIntent?=?PendingIntent.getActivity(context,?0,?intent,?PendingIntent.FLAG_UPDATE_CURRENT); ??
  • ????????notification.setLatestEventInfo(context,?title,?content,?pendIntent); ??
  • ????????notification.flags?|=?Notification.FLAG_AUTO_CANCEL; ??
  • ????????return?notification; ??
  • ????} ??
  • ??
  • ... ??
  • mNotificationManager.notify(ID_1,? ??
  • ????????????????????genreNotification(mContext,?ICON_RES,? ??
  • ????????????????????????????notifyText1,?notifyTitle1,?notifyText1,?intent_1)); ??
  • ... ??
  • mNotificationManager.notify(ID_2,? ??
  • ????????????????????genreNotification(mContext,?ICON_RES,? ??
  • ????????????????????????????notifyText2,?notifyTitle2,?notifyText2,?intent_2)); ??
  • ??
  • ... ??
  • mNotificationManager.notify(ID_3,? ??
  • ????????????????????genreNotification(mContext,?ICON_RES,? ??
  • ????????????????????????????notifyText3,?notifyTitle3,?notifyText3,?intent_3));??
  • private Notification genreNotification(Context context, int icon, String tickerText, String title, String content, Intent intent){Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());PendingIntent pendIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);notification.setLatestEventInfo(context, title, content, pendIntent);notification.flags |= Notification.FLAG_AUTO_CANCEL;return notification;}... mNotificationManager.notify(ID_1, genreNotification(mContext, ICON_RES, notifyText1, notifyTitle1, notifyText1, intent_1)); ... mNotificationManager.notify(ID_2, genreNotification(mContext, ICON_RES, notifyText2, notifyTitle2, notifyText2, intent_2));... mNotificationManager.notify(ID_3, genreNotification(mContext, ICON_RES, notifyText3, notifyTitle3, notifyText3, intent_3));

    ?可見ID和Intent都是不同的,生成的PendingIntent分別對應著不同的Intent。但是,你會發覺無論點哪個Notification,傳遞回來的都是最后被notify的Intent。這里即intent_3。

    ?

    找了很久,試了改變PendingIntent的flag也無果,最后還是在這帖子里找到答案(CSDN帖子 ),我來總結下:

    問題主要出在PendingIntent.getActivity();的第二個參數,API文檔里雖然說是未被使用的參數(給出的例子也直接寫0的),實際上是通過該參數來區別不同的Intent的,如果id相同,就會覆蓋掉之前的Intent了。所以總是獲取到最后一個Intent。

    ?

    只要每個不同的Intent對應傳遞一個獨立的ID就可以了,以上函數修改如下(增加ID參數):

    Java代碼 ?
  • private?Notification?genreNotification(Context?context,?int?icon,?String?tickerText,?String?title,?String?content,?Intent?intent,?int?id){ ??
  • ????????Notification?notification?=?new?Notification(icon,?tickerText,?System.currentTimeMillis()); ??
  • ????????//?問題就在這里的id了 ??
  • ????????PendingIntent?pendIntent?=?PendingIntent.getActivity(context,?id,?intent,?PendingIntent.FLAG_UPDATE_CURRENT); ??
  • ????????notification.setLatestEventInfo(context,?title,?content,?pendIntent); ??
  • ????????notification.flags?|=?Notification.FLAG_AUTO_CANCEL; ??
  • ????????return?notification; ??
  • ????} ??
  • ??
  • ... ??
  • mNotificationManager.notify(ID_1,? ??
  • ????????????????????genreNotification(mContext,?ICON_RES,? ??
  • ????????????????????????????notifyText1,?notifyTitle1,?notifyText1,?intent_1,?ID_1)); ??
  • ... ??
  • mNotificationManager.notify(ID_2,? ??
  • ????????????????????genreNotification(mContext,?ICON_RES,? ??
  • ????????????????????????????notifyText2,?notifyTitle2,?notifyText2,?intent_2,?ID_2)); ??
  • ??
  • ... ??
  • mNotificationManager.notify(ID_3,? ??
  • ????????????????????genreNotification(mContext,?ICON_RES,? ??
  • ????????????????????????????notifyText3,?notifyTitle3,?notifyText3,?intent_3,?ID_3));??
  • 轉載于:https://www.cnblogs.com/wangluochong/p/4189716.html

    總結

    以上是生活随笔為你收集整理的[问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?的全部內容,希望文章能夠幫你解決所遇到的問題。

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