【问题标题】:Show activity only once when clicking the Notification icon单击通知图标时仅显示一次活动
【发布时间】:2012-02-24 20:17:19
【问题描述】:

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

这就是我的工作:

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

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

【问题讨论】:

    标签: java android notifications


    【解决方案1】:

    Manifest 中,将launchModeActivity 设置为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)
    

    您可以在所有标志上查看更多详细信息here(在摘要、常量...下,然后是所有以FLAG_ACTIVITY 开头的标志)。如果我的建议不起作用,我认为您仍然可以在文档中找到答案。

    【讨论】:

    • 感谢您的回复。我仍然无法让它工作,我有点厌倦了尝试组合和组合并花时间在这个问题上。我想我会在用户点击它时从状态栏中删除通知。问候
    【解决方案2】:

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

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

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

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

    这将防止 Activity 有多个实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      • 1970-01-01
      • 2020-03-17
      相关资源
      最近更新 更多