Android的通知推出相同的活动两次 [英] android notification launches same activity twice

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

问题描述

当我点击状态栏上的通知,它将启动一项活动,但行为很奇怪。如果我的应用程序是在前台,我单击该通知原意是触发一次通知。如果我的应用程序在后台则通知的意图是发射两次。如果我退出该应用程序(即所有活动已经弹出通过点击返回按钮),然后通知意向触发一次。任何人都可以解释这种现象。在code片断如下:

  _notification =新的通知(icon_id,股票代码文本,System.currentTimeMillis的());
_showActivityIntent =新意图();
_showActivityIntent.setAction(MyActivityName);
_showActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK + Intent.FLAG_ACTIVITY_NO_HISTORY);
_showActivityPendingIntent = PendingIntent.getActivity(上下文,0,_showActivityIntent,0);
_notification.setLatestEventInfo(背景下,我的冠军,我的文字,_showActivityPendingIntent);
_notificationMgr.notify(notificationId,_notification);
 

解决方案

  _showActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 

试试这个。它将$同一活动的对$ pvent多个实例。 ü可以在清单中把这个也

When I click the notification on the status bar it launches an activity but the behavior is strange. If my app is in foreground and I click the notification the notification intent is fired once. If my app is in background then the notification intent is fired twice. If I exit the app (ie all activities have been popped by hitting the back button) then notification intent is fired once. Can anyone explain this behaviour. The code snippet is as follows:

_notification = new Notification(icon_id, "Ticker Text", System.currentTimeMillis());
_showActivityIntent = new Intent();
_showActivityIntent.setAction(MyActivityName);
_showActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK + Intent.FLAG_ACTIVITY_NO_HISTORY);
_showActivityPendingIntent = PendingIntent.getActivity(context, 0, _showActivityIntent, 0);
_notification.setLatestEventInfo(context, "My title", "My text", _showActivityPendingIntent);
_notificationMgr.notify(notificationId, _notification);

解决方案

_showActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Try this. it will prevent multiple instances of the same activity. u can put this in the manifest also

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

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