Android 通知:需要 Intent.FLAG_ACTIVITY_NEW_TASK? [英] Android Notification: Intent.FLAG_ACTIVITY_NEW_TASK required?

查看:25
本文介绍了Android 通知:需要 Intent.FLAG_ACTIVITY_NEW_TASK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Notification 类的文档中,我看到:

In the document of the Notification class I see this:

公开 PendingIntent contentIntent

单击展开的状态条目时执行的意图.如果这是一个活动,它必须包含 FLAG_ACTIVITY_NEW_TASK 标志,这要求您按照 任务和后台堆栈 文档.特别是,请务必阅读通知部分处理通知 了解从通知启动应用程序的正确方法.

The intent to execute when the expanded status entry is clicked. If this is an activity, it must include the FLAG_ACTIVITY_NEW_TASK flag, which requires that you take care of task management as described in the Tasks and Back Stack document. In particular, make sure to read the notification section Handling Notifications for the correct ways to launch an application from a notification.

我已经阅读了上面链接的材料,但我仍然不明白.为什么要通过单击通知启动活动时需要 FLAG_ACTIVITY_NEW_TASK 标志?我尝试了以下代码:

I've read the materials linked from the above, but I still don't get it. Why is the FLAG_ACTIVITY_NEW_TASK flag required when an activity is to be started from clicking a notification? I tried the following code:

NotificationManager manager = (NotificationManager)context.
    getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(
    android.R.drawable.stat_notify_sync, title,
    System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context, NotifiedActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  // IS THIS REALLY REQUIRED??
PendingIntent pt = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context, title, text, pt);
manager.notify(0, notification);

我运行了上面的代码,有和没有 intent.setFlags 行,似乎没有区别.事实上,我发现的许多代码示例根本没有那一行.那么为什么文档说 FLAG_ACTIVITY_NEW_TASK 标志是必须的,它在通知处理方面究竟有什么区别?

I ran the above code, both with and without the intent.setFlags line, and there seems to be no difference. In fact, many code samples I found simply don't have that line. So why does the document says that the FLAG_ACTIVITY_NEW_TASK flag is a must, and exactly what difference does it make in notification handling?

推荐答案

从技术上讲,这个标志是必需的.但是由于它是必需的,Android 很好,并且会为您设置它;-)

Technically this flag is required. But since it is required, Android is nice and will just set it for you ;-)

需要的原因如下:

处理 Notification 并调用 startActivity() 以实际启动 Intent 的代码,不是在任务"中运行.它是系统代码,是Notification 系统的一部分.通常,如果您调用 startActivity() 并且标志 Intent.FLAG_ACTIVITY_NEW_TASK 未设置,Android 将尝试将该 Activity 启动到当前任务中(即:包含正在调用 startActivity() 的 Activity 的任务).由于在这种情况下没有任务,Android 必须将 Activity 启动到另一个任务中.这就是为什么你需要指定 Intent.FLAG_ACTIVITY_NEW_TASK.

The code that processes the Notification and calls startActivity() to actually launch the Intent, isn't running in a "task". It is system code, part of the Notification system. Normally, if you call startActivity() and the flag Intent.FLAG_ACTIVITY_NEW_TASK is not set, Android will try to launch that Activity into the current task (ie: the task containing the Activity that is calling startActivity()). Since, in this case, there is no task, Android must launch the Activity into another task. That's why you need to specify Intent.FLAG_ACTIVITY_NEW_TASK.

在实践中,这不会总是创建一个新任务,因为 Android 会首先尝试找到一个(合适的)现有任务来启动 Activity.如果您的应用程序已经在运行,Android 只会将 Activity 启动到该任务中.(这不是 100% 正确,还有其他特殊情况可以改变这种逻辑,但我不打算在这里解决它们).

In practice, this won't always create a new task, since Android will first try to find an (suitable) existing task to launch the Activity into. If your app is already running, Android will just launch the Activity into that task. (This isn't 100% true, there are other special cases that can change this logic, but I'm not going to address them here).

注意:如果您从 ServiceBroadcastReceiver 调用 startActivity(),也会存在同样的情况.在这些情况下,标志Intent.FLAG_ACTIVITY_NEW_TASK必须设置,因为没有当前任务",所以Android必须将Activity启动到另一个任务.

NOTE: The same situation exists if you call startActivity() from a Service or a BroadcastReceiver. In those cases, the flag Intent.FLAG_ACTIVITY_NEW_TASK must be set, because there is no "current task", so Android must launch the Activity into another task.

这篇关于Android 通知:需要 Intent.FLAG_ACTIVITY_NEW_TASK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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