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

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

问题描述

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

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)

为什么他们开始了两次?

stackoverflow 上有两个相同的问题:这里此处.但他们没有解释最初的问题可能是什么,他们对我不起作用.例如.更改为 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

意图.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?

我现在正在检查是否已收到类似的 Intent,如果收到,则完成活动.它有效,但看起来不太好.

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?

更新:我可以通过像这样设置 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天全站免登陆