Android 通知PendingIntent意圖開啟Activity,資料無更新? 解決方案

來源:互聯網
上載者:User

當使用Notification通知,使用PendingIntent延遲意圖來開啟Activity,顯示通知的詳情。若有多個通知到來,但意圖Intent等都是一樣的,只是通知的內容不同時,發現多次開啟的通知詳情Activity,顯示的資料居然是第一次的資料,資料沒有更新。比如顯示代碼:  www.2cto.com[java]        //彈出應用自己的通知,在通知欄中顯示xx回複的簡訊結果       private void popNotification(Context context, SmsMsg smsMsg) {      String body = smsMsg.getBody();      String promptContent = null;      if(null!=body&&body.length()>10){          promptContent = body.substring(0, 10);      }else{          promptContent = body;      }      CharSequence charDateTime = DateFormat.format("yyyy-MM-dd kk:mm:ss", new Date(smsMsg.getTime()));      //擷取 NotificationManager      NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);            //建立一個Notification對象      Notification n = new Notification(R.drawable.ic_launcher, "xx簡訊提醒", System.currentTimeMillis());      n.flags = Notification.FLAG_AUTO_CANCEL;  //通知被點擊後,可自動消失      n.defaults |= Notification.DEFAULT_SOUND;  //通知到達時發出預設音樂      n.defaults |= Notification.DEFAULT_VIBRATE; //通知到達時發出預設震動      Intent readMsgIntent = new Intent(context,SmsReadActivity.class);      readMsgIntent.putExtra("SmsMsg", smsMsg); //將收到的簡訊,攜帶到activity中      readMsgIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //在服務中開啟一個Activity要放此標記      //PendingIntent.FLAG_UPDATE_CURRENT 處理當intent攜帶不同的資料時,activity可以get出不同資料。      PendingIntent pi = PendingIntent.getActivity(context, 300, readMsgIntent, 0); //使用者點擊後開啟readMsgIntent指定的activity      n.setLatestEventInfo(context, "xx簡訊提醒 "+charDateTime, promptContent, pi);            nm.notify(10, n);  }   問題出現的代碼就是:PendingIntent pi = PendingIntent.getActivity(context, 300, readMsgIntent, 0); 需要將0改成PendingIntent.FLAG_UPDATE_CURRENT即改成:PendingIntent pi = PendingIntent.getActivity(context, 300, readMsgIntent, PendingIntent.FLAG_UPDATE_CURRENT); //使用者點擊後開啟readMsgIntent指定的activity 根據API對FLAG_UPDATE_CURRENT的解釋如下:Flag for use with getActivity, getBroadcast, andgetService: 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. 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.