Android 单击通知两次启动活动

Android 单击通知两次启动活动,android,android-activity,notifications,Android,Android Activity,Notifications,我正在使用以下代码从服务创建通知: NotificationManager notificationManager = (NotificationManager) ctx .getSystemService(Context.NOTIFICATION_SERVICE); CharSequence tickerText = "bla ..."; long when = System.currentTimeMillis(); Notification notification = new Notif

我正在使用以下代码从服务创建通知:

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_活动_CLEAR_TOP | pendingent.FLAG_UPDATE_CURRENT


但问题依然存在。

这似乎是三星galaxy的一个缺陷。确认在其他设备上一切正常

(另见)

一种解决方法是使用笨拙但有效的技巧取消第二个意图(不确定这是否会对其他设备产生副作用):


因此,请小心使用,并可能检查上一次单击是否是在最后一秒完成的,那么很可能是双触发问题。

同样,三星Galaxy S也出现了问题。有什么好的解决方法吗

我现在正在检查是否已经收到类似的意向,如果收到,请完成活动。它能用,但看起来不太好看

我们能取消第二个意图吗

更新:我可以通过如下设置标志来防止问题:

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

松开或按下按键时呼叫(避免常规操作)


请使用如下所示的标志

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

我从头开始创建一个测试项目,同样的问题也存在(在三星设备上)。对于emulator(2.1+2.2),一切似乎都很好。很好!!谢谢你的确认。这几乎让我发疯:)我将更新我的答案,以找到解决办法
if(event.getAction() == event.ACTION_UP){
   Intent intent = new Intent(......);
   startActivityForResult(intent, 0);
}
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                            |Intent.FLAG_ACTIVITY_SINGLE_TOP);