Android Trick 6: PendingIntent的重複問題,當建立的PendingIntent匹配上一個已經存在的PendingIntent時可能不會建立新的。

來源:互聯網
上載者:User

Android中預設對PendingIntent的建立(如通過PendingIntent.getActivity方式)會進行最佳化檢測,預設的情況下,新建立的PendingIntent如果和原先的基本一樣,那麼就會使用原先的PendingIntent。

這就導致在目前的項目中,一定間隔的重複給使用者發送某類通知,其中帶的Intent裡的Extra內容不同,但是通知點擊後傳到一個Activity裡的extra訊息還是原來的。那麼出現做這種問題可以這樣解決,在建立PendingIntent時,語句改成如下:

 

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,<br />notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

注意PendingIntent.FLAG_UPDATE_CURRENT這個參數,關鍵就是設定了這個參數,在extra發送變化時確定為新的PendingIntent。其他同類參數的用法可進一步參考API文檔。

附上參考網站上發現的解決方案:

http://stackoverflow.com/questions/4340431/how-can-i-correctly-pass-unique-extras-to-a-pending-intent

http://stackoverflow.com/questions/4472447/confusing-behavior-when-starting-new-activity-with-pendingintent

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.