2013-08-27 33 views
1

我想通過使用捆綁通過一個待處理意圖從我的活動從服務傳遞一些值。如果應用程序剛剛啓動,則一切正常,但當應用程序處於恢復模式時,儘管我的服務從遠程服務器接收到新值並將其置於待傳遞給活動,但該活動顯示舊值。下面是服務端的代碼:Android:無法從重新啓動服務替換捆綁

private void sendNotification(String wholemsg) { 
    mNotificationManager = (NotificationManager) this 
      .getSystemService(Context.NOTIFICATION_SERVICE); 



    Intent notificationIntent = new Intent(this, MainActivity.class); 
    notificationIntent.setAction(Intent.ACTION_MAIN); 
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); 

    /* Do something to extract salesID and notificationmessage from wholemsg 
    .... 
    .... 
    .... 
    salesID=.... 
    notificationmessage=... 
    .... 
    */ 


    Bundle bundle=new Bundle(); 
    bundle.putString("msg", notificationmessage); 
    bundle.putString("strsalesID", salesID);  

    notificationIntent.replaceExtras(bundle); 

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
      notificationIntent,  PendingIntent.FLAG_ONE_SHOT+PendingIntent.FLAG_UPDATE_CURRENT); 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
      this) 

    .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("AppV001 Notification") 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationmessage)) 
      .setContentText(notificationmessage); 

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 

} 

這是對我的活動onRestart方法:

  @Override 
     protected void onRestart() { 

    super.onRestart(); 


    NotificationManager mNotificationManager = (NotificationManager) this 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.cancel(1); 
    try { 

     Intent intent = getIntent(); 
     if (intent.hasExtra("msg") && intent.hasExtra("strsalesID")) { 
      String strmsgtitle = intent.getStringExtra("msg"); 
      salesID = intent.getStringExtra("strsalesID"); 
      titletext.setText(getString(R.string.str_ettitle) + salesID); 
     } 
    } catch (Exception ex) { 
     return; 
    } 

} 

的問題是,salesID保持之前的值時,應用程序來自隱藏模式回來。看起來,該服務在隱藏時無法更改活動包。

非常感謝您的時間!

+0

根據什麼launchMode你已經在你的清單這項活動的規定,你可能。需要重寫['onNewIntent()'](http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent))以從服務接收更新的'Intent' – acj

+0

感謝您回覆!我嘗試使用onNewIntent()但它不會觸發事件。這裏是清單: user2723342

回答

0

我發現我的代碼和我想發佈在這裏以防萬一別人面臨同樣的問題時出了什麼問題。 acj是對的,我的啓動模式有問題。我的原始應用程序清單是這樣的:

<activity 
     android:name="com.example.appv001.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

由於沒有聲明啓動模式,所以標準模式是自動進行的。我們可以有四個launchMode (look at this)

'標準' 爲默認值。這四個值分爲兩個組 :

  • 「標準」和「singleTop」可以實例多個活動實例,實例將留在相同的任務。
  • 對於'singleTask'或'singleInstance',活動類使用單例模式,該實例將成爲新任務的根活動。

由於我的活動有標準發射模式下,當服務被調用它的活動的一個新實例被創建和意圖傳遞給這一新的活動。因此,不是調用「onNewIntent()」,而是調用「onCreate」方法,換句話說,「onNewIntent()」從未被調用來設置包等。我將活動清單更改爲此,並設置啓動模式到singleTask(機器人:launchMode =「singleTask),只是解決了這個問題:

<activity 
     android:name="com.example.appv001.MainActivity" 
     android:label="@string/app_name" 
     android:launchMode="singleTask"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

我希望這可以幫助別人