2011-04-04 163 views
11

我創建從服務用下面的代碼的通知:點擊通知開始活動兩次

NotificationManager notificationManager = (NotificationManager) ctx 
.getSystemService(Context.NOTIFICATION_SERVICE); 

CharSequence tickerText = "bla ..."; 
long when = System.currentTimeMillis(); 
Notification notification = new Notification(R.drawable.icon, 
tickerText, when); 

Intent notificationIntent = new Intent(ctx, SearchActivity.class). 
putExtra(SearchActivity.INTENT_SOURCE, 
MyNotificationService.class.getSimpleName()); 

PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, 
notificationIntent, 0); 
notification.setLatestEventInfo(ctx, ctx.getString(R.string.app_name), 
tickerText, contentIntent); 
notification.flags |= Notification.FLAG_AUTO_CANCEL; 
notificationManager.notify(1, notification); 

日誌清楚地說,該方法startActivity被調用的兩倍:

04-02 23:48:06.923: INFO/ActivityManager(2466): Starting activity: Intent { act=android.intent.action.SEARCH cmp=com.xy/.SearchActivity bnds=[0,520][480,616] (has extras) } 
04-02 23:48:06.923: WARN/ActivityManager(2466): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { act=android.intent.action.SEARCH cmp=com.xy/.SearchActivity bnds=[0,520][480,616] (has extras) } 
04-02 23:48:06.958: INFO/ActivityManager(2466): Starting activity: Intent { act=android.intent.action.SEARCH cmp=com.xy/.SearchActivity bnds=[0,0][480,96] (has extras) } 
04-02 23:48:06.958: WARN/ActivityManager(2466): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { act=android.intent.action.SEARCH cmp=com.xy/.SearchActivity bnds=[0,0][480,96] (has extras) } 
04-02 23:48:07.087: INFO/notification(5028): onStartCmd: received start id 2: Intent { cmp=com.xy/.NotificationService } 
04-02 23:48:07.310: INFO/notification(5028): onStartCmd: received start id 3: Intent { cmp=com.xy/.NotificationService } 
04-02 23:48:07.392: INFO/ActivityManager(2466): Displayed activity com.xy/.SearchActivity: 462 ms (total 462 ms) 
04-02 23:48:07.392: INFO/ActivityManager(2466): Displayed activity com.xy/.SearchActivity: 318 ms (total 318 ms) 

他們爲什麼開始兩次?

在計算器上有兩個相同的問題:herehere。 但他們沒有解釋最初的問題可能是什麼,他們不適合我。例如。更改爲launchMode singleTop不適用於我,它應該根據official docs(請參閱調用搜索對話框)在不更改launchMode的情況下工作。

不過我也試過以下標誌添加到notificationIntent

Intent.FLAG_ACTIVITY_CLEAR_TOP | PendingIntent.FLAG_UPDATE_CURRENT

但問題依然存在。

+0

我從頭開始創建一個測試項目,同樣的問題存在那裏(在三星設備上)。對於模擬器(2.1 + 2.2)似乎都很好。 – Karussell 2011-04-05 10:03:24

回答

14

同樣在這裏,三星Galaxy S上出現問題。任何一種解決方法的好主意?

我現在正在檢查是否已經收到類似的Intent並且如果是的話就完成這個活動。它可以工作,但看起來不太好。

我們可以以某種方式取消第二個意圖嗎?

UPDATE:我可以通過設置FLAG_ONE_SHOT這樣防止問題:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT); 
+0

不錯!感謝您確認這一點。這使我幾乎瘋狂:)我會更新我的解決方案的答案 – Karussell 2011-04-08 09:42:40

0

這似乎是一個三星銀河系統的bug。確認在其他設備上一切正常。

(也no discussion at google groups見)

一個解決辦法可能是取消第二意圖的笨拙,但工作訣竅(不知道這對其他設備的副作用):

// put a hint into the notification and check if the intent 
// comes from notification or not: 
String intentSource = queryIntent.getStringExtra(INTENT_SOURCE); 
if (NotificationService.class.getSimpleName().equals(intentSource)) { 
// don't do this again e.g. when rotating! 
queryIntent.removeExtra(INTENT_SOURCE); 

if (getIntent() != null && getIntent().getSourceBounds() != null) { 
    if (getIntent().getSourceBounds().top == 0 
      && getIntent().getSourceBounds().left == 0) { 
     Log.w("search", "Problematic double trigger!"); 
    } 
} 
} 

所以使用這要小心,並可能檢查是否在前一秒完成了前一次點擊,那很可能是雙觸發問題。

1

調用它釋放鍵或按下鍵(一般避免ACTION)!

if(event.getAction() == event.ACTION_UP){ 
    Intent intent = new Intent(......); 
    startActivityForResult(intent, 0); 
} 
5

請使用下列標誌。

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
          |Intent.FLAG_ACTIVITY_SINGLE_TOP); 
+3

這不適用於** PendingIntent ** – swiftBoy 2013-12-16 09:00:36