点击通知会启动活动两次

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)

为什么它们会启动两次?

在stackoverflow上有两个完全相同的问题:这里这里。 但它们没有解释最初的问题可能是什么,而且对我来说也没有用。例如,将launchMode更改为singleTop不适合我,根据官方文档(参见调用搜索对话框),它应该在不更改launchMode的情况下工作。

尽管如此,我还尝试向notificationIntent添加了以下标志:

Intent.FLAG_ACTIVITY_CLEAR_TOP | PendingIntent.FLAG_UPDATE_CURRENT

但问题仍然存在。


我从零开始创建了一个测试项目,同样的问题也存在于那里(在三星设备上)。对于模拟器(2.1+2.2),一切似乎都很正常。 - Karussell
4个回答

16

我也遇到了相同的问题,在三星Galaxy S上发生。有什么好的解决办法吗?

我现在正在检查是否已经收到了类似的Intent,如果是,则关闭Activity。它可以解决问题,但看起来不太好。

我们能否取消第二个Intent吗?

更新:我通过像这样设置FLAG_ONE_SHOT来防止出现问题:

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

太好了!谢谢你确认这个。这让我快要疯了 :) 我会更新我的答案来解决这个问题。 - Karussell

5
请按照以下方式使用标志。
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                            |Intent.FLAG_ACTIVITY_SINGLE_TOP);

1

在释放按键或按下按键时调用它(避免使用通用的ACTION)!

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

0

这似乎是三星Galaxy的一个bug。在另一台设备上确认一切正常。

(另请参阅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!");
    }
}
}

因此,请谨慎使用此功能,并可能检查上一次点击是否在最近一秒内完成,这很可能是双触发问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接