点击通知开始活动两次 [英] Click on notification starts activity twice

查看:197
本文介绍了点击通知开始活动两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建从具有以下code服务的通知:

I'm creating a notification from a service with the following code:

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被称为两倍:

The logs clearly says that the the method startActivity is called twice times:

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)

为什么他们启动了两次?

有计算器上两​​个相同的问题:<一href="http://stackoverflow.com/questions/2326622/starting-activity-through-notification-avoiding-duplicate-activities">here和<一href="http://stackoverflow.com/questions/4333946/android-notification-launches-same-activity-twice">here. 但他们并不能说明什么最初的问题可能是,他们不为我工作。例如。切换到launchMode singleTop不拨给我,它应该工作在不改变launchMode根据官方的文档(参阅调用搜索对话框)。

There are two identical questions on stackoverflow: here and here. But they do not explain what the initial issue could be and they do not work for me. E.g. changing to launchMode singleTop is not appropriated for me and it should work without changing launchMode according to the official docs (see Invoking the search dialog).

不过,我也尝试添加以下标志notificationIntent

Nevertheless I also tried to add the following flags to notificationIntent

Intent.FLAG_ACTIVITY_CLEAR_TOP | PendingIntent.FLAG_UPDATE_CURRENT

Intent.FLAG_ACTIVITY_CLEAR_TOP | PendingIntent.FLAG_UPDATE_CURRENT

但问题是一样的。

推荐答案

同样在这里,在三星Galaxy S.出现问题的解决方法,任何好主意吗?

Same here, problem occurs on Samsung Galaxy S. Any good idea for a workaround?

我现在正在检查是否有类似意图已收到,如果是这样完成的活动。它的工作原理,但它看起来并不很漂亮。

I am now checking if a similar Intent was already received and finish the activity if so. It works but it doesn't look very nice.

我们能以某种方式取消第二个意图是什么?

Can we somehow cancel the second intent?

更新:我可以prevent的问题,通过设置FLAG_ONE_SHOT是这样的:

UPDATE: I could prevent the problem by setting the FLAG_ONE_SHOT like this:

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

这篇关于点击通知开始活动两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆