發通知 PendingIntent 中Intent 內容沒有更新


  1. <
    activity android:name="SkyfileActivity"   
  2.          android:launchMode="singleTask" />  
   當我們把Activity 啓動模式設置爲 singleTask 之後 當我們下次 再去 用Intent 啓動 這個 Activity 的時候 就不會去調用 onCreate方法 而是去調用onNewIntent()方法 然後把Intent中的數據傳給它 , 前幾天遇到的問題是  當我 發一個通知給狀態欄  然後點擊這個通知  自然會執行 PendingIntent 裏邊的Intent。  但是   在Activity那邊的 onNewIntent()方法裏邊 得到的數據  不是最新的 也就是說 是 第一次的 以後 不管我怎麼點通知 它都 是 第一次點擊通知得到的數據,當以後再點擊通知的時候其實 數據已經變了 但是 onNewIntent()方法總是得不到最新的數據,   無語了很久,  去 農民伯伯翻譯組 發問得解    需要給 PendingIntent 加一個 FLAG 

Java代碼  收藏代碼
  1. PendingIntent contentIntentBegin = PendingIntent.getActivity(  
  2.                     notificationContext, 0, inStart, PendingIntent.FLAG_UPDATE_CURRENT);  


   最後一個參數就是 FLAG,這個FLAG 的 意思就是:如果系統中已存在該PendingIntent對象,那麼系統將保留該PendingIntent對象,但是會使用新的Intent來更新之前PendingIntent中的Intent對象數據,例如更新Intent中的Extras。這個非常有用,例如之前提到的,我們需要在每次更新之後更新Intent中的Extras數據,達到在不同時機傳遞給MainActivity不同的參數,實現不同的效果。 

   就是 Intent裏邊的數據沒更新而已, 很2個問題 搞了很久 才發現原來加個FLAG 就行了,有點傷不起了。!! 

  代碼片段 
Java代碼  收藏代碼
  1. public void showNotiMessageBegin(String message, int requestCode,  
  2.         String itemid) {  
  3.     notification = new Notification(R.drawable.skyfile_upload_noti,  
  4.             message, System.currentTimeMillis());  
  5.     if (requestCode == 1 && notification.contentIntent == null) {  
  6.         int index = itemid.lastIndexOf("/");  
  7.         final String backPath1 = itemid  
  8.                 .substring(0, index == 0 ? 1 : index);  
  9.                      
  10.         Intent inStart = new Intent(notificationContext, SkyfileActivity.class);  
  11.         inStart.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  12.         inStart.setData(Uri.parse(MetaDataView.class.getName()));  
  13.         inStart.putExtra(MetaDataView.BUNDLE_PATH, backPath1);  
  14.         PendingIntent contentIntentBegin = PendingIntent.getActivity(  
  15.                 notificationContext, 0, inStart, PendingIntent.FLAG_UPDATE_CURRENT);  
  16.         notification.contentIntent = contentIntentBegin;  
  17.         notification.flags |= Notification.FLAG_AUTO_CANCEL;  
  18.         notification.setLatestEventInfo(notificationContext,  
  19.                 UPLOATTITLE, message, contentIntentBegin);  
  20.         notificationMgr.notify(1, notification);  
  21.     } else {  
  22.         notification.contentIntent.cancel();  
  23.     }  
  24. }  


   更多關於PengingIntent 的 大家可以看看 這裏。 http://www.7dot9.com/2011/04/android-pendingintent%E7%9A%84%E4%B8%80%E4%BA%9B%E5%B0%8F%E8%BF%B7%E6%83%91/
發佈了35 篇原創文章 · 獲贊 5 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章