2014-01-19 34 views
0

後並沒有改變我的這個Activity稱爲Postingcom.emergency.nothingMuch意向行動回顧活動

<activity 
     android:name="Posting" 
     android:label="@string/app_name" 
     android:launchMode="singleTop" 
     android:uiOptions="splitActionBarWhenNarrow"> 
     <intent-filter> 
      <action android:name="com.emergency.StopDistributing"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="com.emergency.nothingMuch"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 
    </activity> 

我使用行動導航到這個特定的活動。如表現在下面的代碼:

i = new Intent("com.emergency.nothingMuch"); 
startActivity(i); 

現在Posting類我有一個Button,單擊時,應該綁定到服務,並從它那裏得到的數據,並做一些任務。通知也會顯示,通過點擊通知,活動將解除綁定服務並執行其他任務。

通知有一個PendingIntent到同樣的Posting類,但有不同的行動。下面的代碼是:

Intent iStop = new Intent("com.emergency.StopDistributing"); 
PendingIntent stopTrackingPI = PendingIntent.getActivity(this, 1, iStop, PendingIntent.FLAG_UPDATE_CURRENT); 
builder.addAction(android.R.drawable.ic_media_pause, "Stop Tracking", stopTrackingPI); 

現在onResume我有一個if語句來檢查的意圖行動。

if(getIntent().getAction().equals("com.emergency.StopDistributing")) { 
     stopPosting(); 
    } 

在if語句之前添加Log.d之後。當我按通知行動。我得到的意圖行動仍然是nothingMuch。它不會更改爲com.emergency.StopDistributing

我也用這個活動使用android:launchMode="singleTop",因爲我想修改現有的活動而不是打開一個新的活動。

那麼爲什麼當點擊通知時Intent的動作沒有變化,我該如何解決它。提前致謝。

P.S:我不想使用setIntent(iStop);改變它,因爲我有其他的PendingIntent s,所以我需要另一種解決方案。

回答

2

嘗試在您的Activity中使用onNewIntent()方法來檢查Intent的操作,而不是使用getIntent(),它仍會返回原始意圖。

這是針對在其包中將launchMode設置爲「singleTop」的活動或者調用startActivity(Intent)時使用FLAG_ACTIVITY_SINGLE_TOP標誌的活動調用的。在這兩種情況下,當活動在活動堆棧頂部重新啓動時,而不是啓動活動的新實例時,將使用用於重新啓動的Intent在現有實例上調用onNewIntent()它。

在接收到新的意圖之前,活動總是會暫停,因此您可以依賴onResume()在此方法之後被調用。

請注意,getIntent()仍然會返回原始的Intent。您可以使用 setIntent(意圖)將其更新爲此新的意圖。

+0

它的工作謝謝你! getIntent()。getAction()返回舊的intent Action。然而'onNewItent()'的意圖將會返回新的Action!感謝您的快速響應。 – Coderji

+0

正是我需要的。我以爲我有一個模糊的問題,但不知道它顯然是超級簡單! – Sirens