如何通过单击通知恢复之前的活动

发布于 2024-09-13 06:24:53 字数 1017 浏览 3 评论 0原文

当我的通知消失时,我想恢复放入后台的活动,而不是启动新活动。我已经看到了一些关于使用 FLAGS 的答案,但我不知道如何实现它

contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | INTENT.FLAG_ACTIVITY_SINGLE_TOP);

我应该把它放在代码中的哪里?我尝试过,但没有成功。请帮忙!

        ns = Context.NOTIFICATION_SERVICE;
        mNotificationManager = (NotificationManager) getSystemService(ns);
        icon = R.drawable.icon;
        tickerText = "Short Msg";
        when = System.currentTimeMillis();
        notification = new Notification(icon, tickerText, when);
        context = getApplicationContext();
        contentTitle = "MyApp";
        contentText = "Reopen App";
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationIntent = new Intent(this, StartTimer.class);
        contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

When my notification goes off I want to restore the activity that was put into the background, not start a new activity. I've seen some answers about using FLAGS but I don't know how to implement it

contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | INTENT.FLAG_ACTIVITY_SINGLE_TOP);

Where do I put this in my code? I tried but it didn't work. Please help!

        ns = Context.NOTIFICATION_SERVICE;
        mNotificationManager = (NotificationManager) getSystemService(ns);
        icon = R.drawable.icon;
        tickerText = "Short Msg";
        when = System.currentTimeMillis();
        notification = new Notification(icon, tickerText, when);
        context = getApplicationContext();
        contentTitle = "MyApp";
        contentText = "Reopen App";
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationIntent = new Intent(this, StartTimer.class);
        contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏夜暖风 2024-09-20 06:24:53

想通了,在 Android Manifest 中将 Activity 设置为 SingleTop 或 SingleInstance,然后它不会创建新的 Activity,而是重新打开仍处于活动状态的 Activity。

Figured it out, set the Activity to SingleTop or SingleInstance in Android Manifest, then instead of creating a new activity it just reopen the one still active.

优雅的叶子 2024-09-20 06:24:53

请注意,标记为正确的答案并不完全正确,因为“singleTop”仍然可能在某些条件下创建您的活动的多个实例。

真正保证在任何条件下创建活动的唯一实例的启动模式是“singleTask”“singleInstance”

这两个选项为您的活动创建一个且唯一的任务,作为任务的根,区别在于“singleInstance”不允许您的活动之上有其他活动,而“singleTask”则允许。

来源:
http://developer.android.com/guide/topics/manifest /activity-element.html#lmode

Note that answer marked as correct is not completely correct as "singleTop" still may create multiple instances of your activity under certain conditions.

The launch modes that really are garanteed to create a UNIQUE instance of your activity at any condition are "singleTask" and "singleInstance".

These two options creates one and only task for your activity being the root of the task, with the difference that "singleInstance" don't allow other activities on top of yours, while "singleTask" does.

Source:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文