自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
activity 启动政策
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
activity 启动行为由相应应用的 AndroidManifest.xml
文件中的启动模式、intent 标志以及调用方提供的 ActivityOptions 定义。使用 ActivityOption#setLaunchDisplayId(int)
可将特定屏幕指定为 activity 启动的目标。
- 默认情况下,activity 与调用方在同一个屏幕上启动。例如,如果未指定其他标志或选项,通过启动器启动的新 Activity 实例应该放置与启动器相同的屏幕上。请务必在启动时使用正确的上下文(activity 与应用)。
- 如果在与特定屏幕无关的来源执行启动(例如通过 shell 或应用上下文),那么 activity 会位于用户上次与设备互动或上一个 activity 启动时的顶部屏幕上。
- 用于启动 activity 的 intent 可解析为系统中已有的 activity 实例。在这种情况下,如果未提供其他标志,activity 会显示在上次使用它时所在的屏幕上。如果使用
ActivityOptions#setTargetDisplayId()
指定目标屏幕,activity 会移动到该屏幕(如果安全限制及其他限制允许的话)。
安全限制
在 Android 10 中,为了防止恶意应用通过从其创建的虚拟屏幕的表面读取用户敏感信息来盗用相关信息,应用只能在其创建的虚拟屏幕上启动其自己的 Activity。但是:
- 具有
INTERNAL_SYSTEM_WINDOW
权限的系统组件可以在任何屏幕上启动。
- 具有
ACTIVITY_EMBEDDING
权限的调用方可以从具有 ActivityInfo.FLAG_ALLOW_EMBEDDED
标志的其他应用启动 activity。
- 对于专有屏幕,只有其所有者或已在此类屏幕上的 activity 能够在此类屏幕上启动 activity。
向屏幕添加窗口会受到类似限制。
Android 10 包含 ActivityManager#isActivityStartAllowedOnDisplay(Context context, int
displayId, Intent intent)
方法,用于先检查应用的安全限制,然后再尝试在屏幕上启动。在 Android 9(及更低版本)中,如果启动受到限制,系统会抛出 SecurityException
。
大多数安全限制都在 ActivityStackSupervisor#isCallerAllowedToLaunchOnDisplay()
方法中实现。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# Activity launch policy\n\n*Activity launch behavior* is defined by launch modes in the\n`AndroidManifest.xml` files of apps, intent flags, and ActivityOptions\nprovided by the caller. Use `ActivityOption#setLaunchDisplayId(int)` to\ntarget a specific display for activity launch.\n\n- By default, the activity launches on the same display as the caller. For example, a new instance of an activity started from the launcher should be placed on the same display without additional flags or options. Be sure to use the correct context (Activity versus Application) for launch.\n- If the launch is performed from a source not associated with a specific display (such as from a shell or Application context), then the activity is placed on the top display on which the user last interacted with the device or from which the last activity was launched.\n- Intent to launch an activity can be resolved to an existing activity instance in the system. In such a case, if no additional flags were provided, an activity surfaces on the same display where it was last used. If the target display is specified with `ActivityOptions#setTargetDisplayId()`, then the activity is moved to that display (if allowed by security and other restrictions).\n\nSecurity restrictions\n---------------------\n\nTo prevent a malicious app from misappropriating user-sensitive information\nby reading it from the surface of a virtual display that it has created, apps can\nlaunch their own activities *only* on a virtual display of their creation\nin Android 10. However:\n\n- System components with the `INTERNAL_SYSTEM_WINDOW` permission can launch on any display.\n- Callers with the `ACTIVITY_EMBEDDING` permission can launch activities from other apps that have the flag `ActivityInfo.FLAG_ALLOW_EMBEDDED`.\n- Activity launches on private displays are allowed only for the owner or activities present on that display.\n\nSimilar restrictions apply to adding windows to displays.\n\nAndroid 10 includes the\n`ActivityManager#isActivityStartAllowedOnDisplay(Context context, int\ndisplayId, Intent intent)` method to check security restrictions for the\napp before attempting to launch on a display. In Android 9\n(and lower), restricted launch results throw `SecurityException`.\n\nMost security restrictions are applied in the\n`ActivityStackSupervisor#isCallerAllowedToLaunchOnDisplay()` method."]]