android PendingIntent 使用通知傳遞多個參數,及不覆蓋的問題

來源:互聯網
上載者:User

標籤:android   blog   io   os   使用   java   ar   資料   div   

Intent updateIntent = new Intent(GetNoticeService.this,DetailGonggaoActivity.class);updateIntent.putExtra("type", "weidu");updateIntent.putExtra("title",(String) json.get("title"));updateIntent.putExtra("id", json.get("noticeid").toString());//加上這句就不會出現同一個activity出現多次的情況了updateIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//下面有對個參數,是設定的關鍵PendingIntent updatePendingIntent = PendingIntent.getActivity(GetNoticeService.this, i, updateIntent,PendingIntent.FLAG_UPDATE_CURRENT);Notification updateNotification = new Notification();// 設定通知欄顯示內容updateNotification.icon = R.drawable.logo;updateNotification.flags |= updateNotification.FLAG_AUTO_CANCEL;updateNotification.tickerText = "外勤精靈發來新的公告通知";updateNotification.defaults = Notification.DEFAULT_SOUND;// 鈴聲提醒updateNotification.setLatestEventInfo(GetNoticeService.this,"外勤精靈發來新的公告通知", (String) json.get("title"),updatePendingIntent);updateNotificationManager.notify(i, updateNotification);

  

//加上這句就不會出現同一個activity出現多次的情況了,不解釋updateIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

updateNotificationManager.notify(i, updateNotification);這句傳遞的i標記了updateNotification的位置和id相同的時候就覆蓋原來的updateNotification,所以要傳遞不同的id,使不同的updateNotification不覆蓋。

PendingIntent updatePendingIntent = PendingIntent.getActivity(GetNoticeService.this, i, updateIntent,PendingIntent.FLAG_UPDATE_CURRENT);Notification updateNotification = new Notification();
flags有四個取值:
int FLAG_CANCEL_CURRENT:如果該PendingIntent已經存在,則在產生新的之前取消當前的。
int FLAG_NO_CREATE:如果該PendingIntent不存在,直接返回null而不是建立一個PendingIntent.
int FLAG_ONE_SHOT:該PendingIntent只能用一次,在send()方法執行後,自動取消。
int FLAG_UPDATE_CURRENT:如果該PendingIntent已經存在,則用新傳入的Intent更新當前的資料。

我們需要把最後一個參數改為PendingIntent.FLAG_UPDATE_CURRENT,這樣在啟動的Activity裡就可以用接收Intent傳送資料的方法正常接收。

前面的id作用一樣



android 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.