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

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

问题描述

我有一个保持连接打开的服务器,只要用户登录到应用程序前台服务。这是为了让连接保持活动状态并可以直接从服务器接收邮件,即使该应用程序已发送到后台用户pressing首页。

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.

推荐答案

你需要的仅仅是一个简单的活动,什么都不做。下面是一个例子:

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();
    }
}

设置您的通知启动该活动。确保本次活动的清单中的任务亲和力是一样的其他活动,你的应用程序中的任务亲和力(默认情况下它是,如果你还没有明确设置机器人:taskAffinity

当用户选择该通知,如果您的应用程序正在运行,则NotificationActivity将启动在上面的应用程序的任务最上面的活动和任务将被置于前台。当NotificationActivity完成,这将直接返回用户最上面的活动在你的应用程序(即:无论用户在哪里离开它,当它走进背景)。

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).

如果您的应用程序尚未运行这将无法正常工作。但是,你有两个选择来处理是:

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

  1. 确认该通知不是present在通知栏,当你的应用程序没有运行。

  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天全站免登陆