Android 通知:Intent.FLAG_ACTIVITY_NEW_TASK 需要?
Android Notification: Intent.FLAG_ACTIVITY_NEW_TASK required?
在 Notification
class 的文档中我看到了这个:
public PendingIntent contentIntent
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.
我已经阅读了上面链接的资料,但我还是不明白。为什么要从单击通知开始 activity 时需要 FLAG_ACTIVITY_NEW_TASK
标志?我尝试了以下代码:
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
标志是必须的,它在通知处理方面究竟有什么区别?
从技术上讲,此标志是必需的。但是因为它是必需的,所以 Android 很好并且会为你设置它;-)
需要的原因如下:
处理 Notification
并调用 startActivity()
以实际启动 Intent
的代码不是 "task" 中的 运行。它是系统代码,是 Notification
系统的一部分。通常,如果您调用 startActivity()
并且 Intent.FLAG_ACTIVITY_NEW_TASK
标志 未设置 ,Android 将尝试将 Activity 启动到当前任务(即:包含正在调用 startActivity()
的 Activity 的任务)。因为,在这种情况下,没有任务,Android 必须将Activity启动到另一个任务中。这就是为什么您需要指定 Intent.FLAG_ACTIVITY_NEW_TASK
.
实际上,这不会总是创建一个新任务,因为Android会首先尝试找到一个(合适的)现有任务来启动Activity成。如果您的应用程序已经是 运行,Android 只会将 Activity 启动到该任务中。 (这不是 100% 正确,还有其他特殊情况可以改变这个逻辑,但我不打算在这里解决它们)。
注意:如果您从 Service
或 BroadcastReceiver
调用 startActivity()
,也会出现同样的情况。在那些情况下,标志Intent.FLAG_ACTIVITY_NEW_TASK
必须设置,因为没有"current task",所以Android 必须[=38] =] 将 Activity 启动到另一个任务中。
在 Notification
class 的文档中我看到了这个:
public PendingIntent contentIntent
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.
我已经阅读了上面链接的资料,但我还是不明白。为什么要从单击通知开始 activity 时需要 FLAG_ACTIVITY_NEW_TASK
标志?我尝试了以下代码:
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
标志是必须的,它在通知处理方面究竟有什么区别?
从技术上讲,此标志是必需的。但是因为它是必需的,所以 Android 很好并且会为你设置它;-)
需要的原因如下:
处理 Notification
并调用 startActivity()
以实际启动 Intent
的代码不是 "task" 中的 运行。它是系统代码,是 Notification
系统的一部分。通常,如果您调用 startActivity()
并且 Intent.FLAG_ACTIVITY_NEW_TASK
标志 未设置 ,Android 将尝试将 Activity 启动到当前任务(即:包含正在调用 startActivity()
的 Activity 的任务)。因为,在这种情况下,没有任务,Android 必须将Activity启动到另一个任务中。这就是为什么您需要指定 Intent.FLAG_ACTIVITY_NEW_TASK
.
实际上,这不会总是创建一个新任务,因为Android会首先尝试找到一个(合适的)现有任务来启动Activity成。如果您的应用程序已经是 运行,Android 只会将 Activity 启动到该任务中。 (这不是 100% 正确,还有其他特殊情况可以改变这个逻辑,但我不打算在这里解决它们)。
注意:如果您从 Service
或 BroadcastReceiver
调用 startActivity()
,也会出现同样的情况。在那些情况下,标志Intent.FLAG_ACTIVITY_NEW_TASK
必须设置,因为没有"current task",所以Android 必须[=38] =] 将 Activity 启动到另一个任务中。