单击“通知”图标时仅显示一次活动

时间:2012-02-24 20:17:19

标签: java android notifications

我有一个应用程序可以通过某种提醒来提醒用户。这些提醒显示为通知。当用户点击任何这些通知时,它应显示有关该警报的其他信息。这基本上是通过调用具有特定参数的活动来完成的。

这就是我的所作所为:

NotificationManager mNotificationManager = (NotificationManager) MyApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);                 
Notification notification = new Notification(R.drawable.reminder2, reminder.Title, System.currentTimeMillis());

Intent notificationIntent = null;
notificationIntent = new Intent(MyApplication.getContext(), ReminderActivity.class);
notificationIntent.putExtra("idnotification", reminder.ID);


PendingIntent contentIntent = PendingIntent.getActivity(MyApplication.getContext(), reminder.ID, notificationIntent, 0 );

notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.FLAG_INSISTENT;


notification.setLatestEventInfo(MyApplication.getContext(), "Reminder", reminder.Title, contentIntent);

mNotificationManager.notify(reminder.ID, notification);

这似乎有效,问题是如果我在通知上点击多次,它将创建一个新活动而不是显示已经可见的活动。

我尝试为PendingIntent设置标志,但没有运气。我尝试使用FLAG_CANCEL_CURRENT,FLAG_NO_CREATE,FLAG_ONE_SHOT,FLAG_UPDATE_CURRENT。唯一接近我需要的是FLAG_ONE_SHOT,但是第二次点击它不会显示活动。

我还能尝试什么?我在这里完全没有想法。 感谢

2 个答案:

答案 0 :(得分:1)

Manifest中将launchMode的{​​{1}}设置为Activity。如果这不起作用,请尝试singleTop。我认为其中一个可能更适合你。

如果这不起作用,请尝试弄乱singleInstance的一些标志:

Intent

我会尝试以下标志来查看哪些有效,甚至可能使用多个:

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)

您可以在所有标记上查看更多详细信息here(在摘要,常量...下,然后是以FLAG_ACTIVITY_CLEAR_TOP FLAG_ACTIVITY_RESET_TASK_IF_NEEDED FLAG_ACTIVITY_CLEAR_TASK FLAG_ACTIVITY_NEW_TASK (in conjunction with others) 开头的所有标记)。如果我的建议不起作用,我想你仍然会在文档中找到你的答案。

答案 1 :(得分:0)

如果FLAG_UPDATE_CURRENT不起作用,请尝试将androidManifest.xml文件中的活动启动模式从标准更改为singleTask。

默认模式为标准。它在开始活动的任务中创建活动的新实例。可以创建活动的多个实例,并且可以将多个实例添加到相同或不同的任务中。

但是,在 singleTask 启动模式下,将始终创建一个新任务,并且将新实例作为根实例推送到该任务。如果活动任务的实例存在于单独的任务中,则不会创建新实例,并且Android系统通过onNewIntent()方法路由意图信息。

<activity>
...
android:launchMode="singleTask
...
</activity>

这将防止活动具有多个实例。

相关问题