通知恢复任务而不是特定活动? [英] Notification to restore a task rather than a specific activity?

查看:22
本文介绍了通知恢复任务而不是特定活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个前台服务,只要用户登录到应用程序,它就会保持与服务器的连接.这是为了使连接保持活动状态,即使用户按下 Home 键将应用程序发送到后台,也可以直接从服务器接收消息.

I have a foreground service that keeps a connection open with the server as long as the user is logged into the application. This is so that the connection is kept alive and can receive messages directly from the server even when the application has been sent into the background by the user pressing Home.

应用程序有许多活动,当它被发送到后台时,任何活动都可能是活动的.

The application has a number of Activities, any of which could be the active one when it is sent into the background.

我想允许用户点击通知来恢复当前活动.我了解如何恢复特定活动,但想知道是否有办法恢复用户上次使用的活动?当然我可以跟踪最后一个,然后从通知回调中调用它,但认为在任务级别可能有办法吗?

I would like to allow the user to click on the notification to restore the current Activity. I understand how to restore a particular activity, but wondered if there is a way to restore the last Activity that the user was on? Of course I could keep track of the the last one, and then call that from the Notification callback, but thought there might be a way at a task level?

感谢您提供的任何建议.

Thanks for any advice you can offer.

推荐答案

您需要的只是一个什么都不做的简单 Activity.下面是一个例子:

What you need is just a simple Activity that does nothing. Here is an example:

public class NotificationActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Now finish, which will drop the user in to the activity that was at the top
        //  of the task stack
        finish();
    }
}

设置您的通知以开始此活动.确保清单中此活动的任务关联与应用程序中其他活动的任务关联相同(默认情况下,如果您尚未明确设置 android:taskAffinity).

Set up your notification to start this activity. Make sure that in the manifest the task affinity of this activity is the same as the task affinity of the other activities in your application (by default it is, if you haven't explicitly set android:taskAffinity).

当用户选择此通知时,如果您的应用程序正在运行,则 NotificationActivity 将在应用程序任务的最顶层活动的顶部启动,并且该任务将被置于前台.当 NotificationActivity 完成时,它会简单地将用户返回到应用程序中最顶层的 Activity(即:当它进入后台时用户离开它的地方).

When the user selects this notification, if your application is running, then the NotificationActivity will be started on top of the topmost activity in your application's task and that task will be brought to the foreground. When the NotificationActivity finishes, it will simply return the user to the topmost activity in your application (ie: wherever the user left it when it went into the background).

如果您的应用程序尚未运行,这将不起作用.但是,您有 2 个选项可以处理此问题:

This won't work if your application isn't already running. However, you have 2 options to deal with that:

  1. 确保在您的应用程序未运行时通知栏中不存在通知.

  1. Make sure the notification isn't present in the notification bar when your application is not running.

在 NotificationActivity 的 onCreate() 方法中,检查您的应用程序是否正在运行,如果它没有运行,请调用 startActivity() 并启动您的应用程序.如果这样做,请确保在启动应用程序时设置标志 Intent.FLAG_ACTIVITY_NEW_TASK,以便任务的根活动不是 NotificationActivity.

In the onCreate() method of the NotificationActivity, check if your application is running, and if it isn't running call startActivity() and launch your application. If you do this, be sure to set the flag Intent.FLAG_ACTIVITY_NEW_TASK when starting the application so that the root activity of the task is not NotificationActivity.

这篇关于通知恢复任务而不是特定活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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