繁体   English   中英

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

[英]Show activity only once when clicking the Notification icon

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

这就是我所做的:

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,但是我第二次单击它时没有显示活动。

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

Manifest ,将该ActivitylaunchMode设置为singleTop 如果这不起作用,请尝试singleInstance 我认为其中之一可能更适用于您。

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

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)

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

FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_NEW_TASK (in conjunction with others)

你可以看到更多的细节在这里的所有标志(下总结,Contstants ......然后所有以启动标志FLAG_ACTIVITY )。 如果我的建议不起作用,我想您仍然可以在文档中找到答案。

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM