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

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

问题描述

通知类我看到的文件:

公开的PendingIntent contentIntent

点击展开的状态条目时的意图执行。如果这是一个活动,它必须包括 FLAG_ACTIVITY_NEW_TASK 标记,这就要求作为的任务和返回堆栈文件。特别是,一定要阅读通知部分<一个href=\"http://developer.android.com/guide/topics/ui/notifiers/notifications.html#HandlingNotifications\"相对=nofollow>处理通知的正确的方式,推出从通知的应用程序。

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 标志?我尝试以下code:

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

我跑了超过code,既没有在 intent.setFlags 行,似乎有没有什么区别。事实上,我发现很多简单的code样本不具有该行。那么,为什么该文件说, 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 ;-)

它是必需的原因如下:

在code,它处理通知键,通话 startActivity()实际启动意图,是不是在任务的运行。它是系统code时,通知系统的一部分。通常情况下,如果调用 startActivity()和标志 Intent.FLAG_ACTIVITY_NEW_TASK 未设置 ,Android将尝试推出这项活动到当前的任务(即:包含调用该活动任务 startActivity())。因为在这种情况下,是没有任务,Android的必须启动活动到另一个任务。这就是为什么你需要指定 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将首先尝试找到(合适的)现有任务开展活动之中。如果您的应用程序已经在运行,Android将只需启动活动到该任务。 (这不是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).

请注意:同样的情况存在,如果调用 startActivity()服务广播接收器。在这种情况下,该标志 Intent.FLAG_ACTIVITY_NEW_TASK 必须设置后,因为没有当前任务,因此Android 必须启动活动到另一个任务。

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