android – 点击通知开始活动两次

前端之家收集整理的这篇文章主要介绍了android – 点击通知开始活动两次前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用以下代码从服务创建通知
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,notificationIntent,0);
notification.setLatestEventInfo(ctx,ctx.getString(R.string.app_name),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,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,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有两个相同的问题:herehere.
但是他们并不解释最初的问题是什么,而且对我来说并不奏效.例如.更改为launchMode singleTop不适用于我,它应该在没有根据official docs更改launchMode的情况下工作(请参阅调用搜索对话框).

不过我也试图将以下标志添加到notificationIntent中

Intent.FLAG_ACTIVITY_CLEAR_TOP | PendingIntent.FLAG_UPDATE_CURRENT

但问题依然如此.

解决方法

同样在这里,三星Galaxy S出现问题.解决方法有什么好主意?

我现在正在检查是否已经收到了类似的意图,如果是,则完成该活动.它工作,但它看起来不是很好.

我们可以以某种方式取消第二种意图吗?

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

PendingIntent pendingIntent = PendingIntent.getActivity(this,intent,PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
原文链接:https://www.f2er.com/android/313291.html

猜你在找的Android相关文章