行为变更:以 Android 15 或更高版本为目标平台的应用

与以前的版本一样,Android 15 包含一些行为变更,这些变更可能会影响您的应用。以下行为变更仅影响以 Android 15 或更高版本为目标平台的应用。如果您的应用以 Android 15 或更高版本为目标平台,您应该修改自己的应用以适当地支持这些行为(如果适用)。

此外,请务必查看对 Android 15 上运行的所有应用都有影响的行为变更列表(无论应用的 targetSdkVersion 如何)。

核心功能

Android 15 修改或扩展了 Android 系统的各种核心功能。

前台服务的变更

我们将对 Android 15 中的前台服务做出以下更改。

数据同步前台服务超时行为

Android 15 对于以 Android 15 或更高版本为目标平台的应用,在 dataSync 中引入了新的超时行为。此行为也适用于新的 mediaProcessing 前台服务类型

系统允许应用的 dataSync 服务在 24 小时内运行总共 6 小时,之后系统会调用正在运行的服务的 Service.onTimeout(int, int) 方法(在 Android 15 中引入)。此时,服务只有几秒钟的时间来调用 Service.stopSelf()。当调用 Service.onTimeout() 时,该服务不再被视为前台服务。如果服务未调用 Service.stopSelf(),则会发生故障,并显示以下错误消息:“A for 前台服务 (<fgs_type> does not stop in their timeout: <component_name>)”。在 Beta 版 2 中,失败消息显示为 ANR,但在未来的 Beta 版中,此失败消息将抛出自定义异常。

为避免此行为变更出现问题,您可以执行以下一项或多项操作:

  1. 让您的服务实现新的 Service.onTimeout(int, int) 方法。当应用收到回调时,请确保在几秒钟内调用 stopSelf()。(如果您没有立即停止应用,系统会生成故障。)
  2. 确保应用的 dataSync 服务在任何 24 小时内的运行时间总共不超过 6 小时(除非用户与应用互动,请重置计时器)。
  3. 仅因直接用户互动而启动 dataSync 前台服务;由于您的应用在服务启动时正处于前台,因此在它进入后台后,您的服务有整整六个小时的时间。
  4. 请使用替代 API,而不要使用 dataSync 前台服务。

如果应用的 dataSync 前台服务在过去 24 小时内运行了 6 小时,那么除非用户已将您的应用带到前台(这会重置计时器),否则您无法启动其他 dataSync 前台服务。如果您尝试启动另一个 dataSync 前台服务,系统会抛出 ForegroundServiceStartNotAllowedException,并显示“时间限制已用完前台服务类型 dataSync 的时间限制”这样的错误消息。

新增媒体处理前台服务类型

Android 15 引入了一种新的前台服务类型 mediaProcessing。此服务类型适用于对媒体文件进行转码等操作。例如,媒体应用可能下载音频文件,并且需要将其转换为其他格式才能播放。您可以使用 mediaProcessing 前台服务来确保即使应用在后台运行,转换也会继续进行。

系统允许应用的 mediaProcessing 服务在 24 小时内总共运行 6 小时,之后系统会调用正在运行的服务的 Service.onTimeout(int, int) 方法(在 Android 15 中引入)。此时,服务只有几秒钟的时间来调用 Service.stopSelf()。如果服务未调用 Service.stopSelf(),则会发生故障,并显示以下错误消息:“A for the preground service of <fgs_type> was not stop in their timeout: <component_name>”。在 Beta 版 2 中,失败消息显示为 ANR,但在未来的 Beta 版中,此失败消息将抛出自定义异常。

为避免 ANR,您可以执行以下任一操作:

  1. 让您的服务实现新的 Service.onTimeout(int, int) 方法。当应用收到回调时,请确保在几秒钟内调用 stopSelf()。(如果您没有立即停止应用,系统会生成故障。)
  2. 确保应用的 mediaProcessing 服务在任何 24 小时内的运行时间总计不超过 6 小时(除非用户与应用互动,请重置计时器)。
  3. 仅因直接用户互动而启动 mediaProcessing 前台服务;由于您的应用在服务启动时正处于前台,因此在它进入后台后,您的服务有整整六个小时的时间。
  4. 请使用替代 API(例如 WorkManager),而不要使用 mediaProcessing 前台服务。

如果应用的 mediaProcessing 前台服务在过去 24 天内已经运行了 6 小时,那么除非用户已将您的应用带到前台(这会重置计时器),否则您无法启动其他 mediaProcessing 前台服务。如果您尝试启动另一个 mediaProcessing 前台服务,系统会抛出 ForegroundServiceStartNotAllowedException,并显示“时间限制已用尽前台服务类型 mediaProcessing”这样的错误消息。

如需详细了解 mediaProcessing 服务类型,请参阅 Android 15 前台服务类型的变更:媒体处理

对启动前台服务的 BOOT_COMPLETED 广播接收器的限制

针对启动前台服务的 BOOT_COMPLETED 广播接收器存在新的限制。BOOT_COMPLETED 接收器不能启动以下类型的前台服务:

如果 BOOT_COMPLETED 接收器尝试启动任何此类前台服务,系统会抛出 ForegroundServiceStartNotAllowedException

限制在应用拥有 SYSTEM_ALERT_WINDOW 权限的情况下启动前台服务

Previously, if an app held the SYSTEM_ALERT_WINDOW permission, it could launch a foreground service even if the app was currently in the background (as discussed in exemptions from background start restrictions).

If an app targets Android 15, this exemption is now narrower. The app now needs to have the SYSTEM_ALERT_WINDOW permission and also have a visible overlay window. That is, the app needs to first launch a TYPE_APPLICATION_OVERLAY window and the window needs to be visible before you start a foreground service.

If your app attempts to start a foreground service from the background without meeting these new requirements (and it does not have some other exemption), the system throws ForegroundServiceStartNotAllowedException.

If your app declares the SYSTEM_ALERT_WINDOW permission and launches foreground services from the background, it may be affected by this change. If your app gets a ForegroundServiceStartNotAllowedException, check your app's order of operations and make sure your app already has an active overlay window before it attempts to start a foreground service from the background. You can check if your overlay window is currently visible by calling View.getWindowVisibility(), or you can override View.onWindowVisibilityChanged() to get notified whenever the visibility changes.

Testing

To test your app's behavior, you can enable these new restrictions even if your app is not targeting Android 15 (as long as the app is running on an Android 15 device). To enable these new restrictions on starting foreground services from the background, run the following adb command:

adb shell am compat enable FGS_SAW_RESTRICTIONS your-package-name

更改了应用何时可以修改勿扰模式的全局状态

以 Android 15 为目标平台的应用无法再更改设备上的全局状态或勿扰 (DND) 政策(通过修改用户设置或关闭 DND 模式)。相反,应用必须提供一个 AutomaticZenRule,系统会将后者合并到一个具有现有最严格的政策胜出方案的全局政策中。调用之前影响全局状态的现有 API(setInterruptionFiltersetNotificationPolicy)会导致创建或更新隐式 AutomaticZenRule,该 AutomaticZenRule 根据这些 API 调用的调用周期而开启或关闭。

请注意,只有在应用调用 setInterruptionFilter(INTERRUPTION_FILTER_ALL) 且预期调用会停用之前由其所有者激活的 AutomaticZenRule 时,此变更才会影响可观察的行为。

OpenJDK 17 变更

Android 15 会继续更新 Android 的核心库,以与最新 OpenJDK LTS 版本中的功能保持一致。

对于以 Android 15 为目标平台的应用,以下变更可能会影响应用兼容性:

  • 字符串格式设置 API 的变化:在使用以下 String.format()Formatter.format() API 时,现在对参数索引、标志、宽度和精度的验证更为严格:

    例如,使用参数索引 0 时(格式字符串中的 %0)会抛出以下异常:

    IllegalFormatArgumentIndexException: Illegal format argument index = 0
    

    在这种情况下,可以通过在格式字符串中使用 %1 的参数索引 1 来解决此问题。

  • Arrays.asList(...).toArray() 组件类型的更改:使用 Arrays.asList(...).toArray() 时,所生成数组的组件类型现在是 Object,而不是底层数组元素的类型。因此,以下代码会抛出 ClassCastException

    String[] elements = (String[]) Arrays.asList("one", "two").toArray();
    

    在这种情况下,若要在结果数组中保留 String 的组成部分类型,您可以改用 Collection.toArray(Object[])

    String[] elements = Arrays.asList("two", "one").toArray(new String[0]);
    
  • 语言代码处理方面的变更:使用 Locale API 时,希伯来语、意第绪语和印度尼西亚语的语言代码不再转换为过时的形式(希伯来语:iw、意第绪语:ji 和印度尼西亚语:in)。在为其中一个语言区域指定语言代码时,请改用 ISO 639-1 中的代码:yi、Yid1(希伯来语):yiheid

  • 随机 int 序列变更:在 https://bugs.openjdk.org/browse/JDK-8301574 中做出更改后,以下 Random.ints() 方法现在返回的数字序列现在与 Random.nextInt() 方法不同:

    通常,此变更不会导致应用破坏行为,但您的代码不应期望从 Random.ints() 方法生成的序列与 Random.nextInt() 匹配。

安全

Android 15 包含一些有助于提升系统安全性的变更,以帮助保护应用和用户免受恶意应用的侵害。

安全的后台 activity 启动

Android 15 通过添加变更来防止恶意后台应用将其他应用带到前台、提升其权限和滥用用户互动,从而保护用户免受恶意应用的侵害,并使用户能够更好地控制其设备。从 Android 10(API 级别 29)开始,后台 activity 启动已受到限制。

阻止与堆栈中顶部 UID 不匹配的应用启动 activity

恶意应用可以在同一任务内启动另一个应用的 activity,然后叠加在上面,制造出作为该应用的错觉。这种“任务劫持”攻击绕过了当前的后台启动限制,因为它都发生在同一可见任务内。为了降低这种风险,Android 15 添加了一个标志,用于阻止与堆栈上的顶级 UID 不匹配的应用启动 activity。如需选择启用应用的所有 activity,请更新应用的 AndroidManifest.xml 文件中的 allowCrossUidActivitySwitchFromBelow 属性:

<application android:allowCrossUidActivitySwitchFromBelow="false" >

如果满足以下所有条件,则新的安全措施就会生效:

  • 执行启动的应用以 Android 15 为目标平台。
  • 任务堆栈顶部的应用以 Android 15 为目标平台。
  • 所有可见的活动均已启用新版保护措施

如果启用了安全措施,应用如果完成自己的任务,可能会返回主屏幕,而不是返回上一个可见的应用。

其他变更

除了 UID 匹配的限制之外,还包含以下其他更改:

  • PendingIntent 创建者更改为默认阻止后台 activity 启动。这有助于防止应用意外创建可能会被恶意操作者滥用的 PendingIntent
  • 请勿将应用置于前台,除非 PendingIntent 发送者允许该应用。此变更旨在防止恶意应用滥用在后台启动 activity 的功能。默认情况下,不允许应用将任务堆栈置于前台,除非创建者允许后台 activity 启动特权或发送者具有后台 activity 启动特权。
  • 控制任务堆栈的顶层 activity 完成其任务的方式。如果顶层 activity 完成某项任务,Android 会返回到上次处于活动状态的任务。此外,如果非顶层 activity 完成其任务,Android 将返回主屏幕;它不会阻止此非顶部 activity 的完成。
  • 防止将其他应用中的任意 activity 启动到您自己的任务中。这项变更会通过创建看似来自其他应用的活动,防止恶意应用向用户进行钓鱼式攻击。
  • 阻止系统考虑将不可见窗口用于后台 activity 启动。这有助于防止恶意应用滥用后台 activity 启动来向用户显示不受欢迎或恶意内容。

更安全的 intent

Android 15 引入了新的安全措施,旨在提高 intent 的安全性和稳健性。这些更改旨在防范潜在漏洞和滥用被恶意应用利用的 intent。Android 15 对 intent 的安全性做出了两项主要改进:

  • 匹配目标 intent 过滤器:以特定组件为目标的 intent 必须准确匹配目标的 intent 过滤器规范。如果您发送 intent 来启动其他应用的 activity,则目标 intent 组件需要与接收 activity 声明的 intent 过滤器保持一致。
  • intent 必须有操作:没有操作的 intent 将不再匹配任何 intent 过滤器。这意味着,用于启动 activity 或服务的 intent 必须具有明确定义的操作。

Kotlin


fun onCreate() {
    StrictMode.setVmPolicy(VmPolicy.Builder()
        .detectUnsafeIntentLaunch()
        .build()
    )
}

Java


public void onCreate() {
    StrictMode.setVmPolicy(new VmPolicy.Builder()
            .detectUnsafeIntentLaunch()
            .build());
}

用户体验和系统界面

Android 15 包含一些旨在打造更一致、更直观的用户体验的变更。

窗口边衬区更改

Android 15 中有两个与窗口边衬区相关的变更:默认强制执行无边框模式;还存在配置变更,例如系统栏的默认配置。

全面强制执行

如果应用以 Android 15 为目标平台,则在搭载 Android 15 的设备上默认采用无边框应用。

应用以 Android 14 为目标平台且在 Android 15 设备上并非全屏显示的应用。


一个以 Android 15 为目标平台且在 Android 15 设备上实现无边框的应用。此应用主要使用可自动应用边衬区的 Material 3 Compose 组件。此屏幕不会受到 Android 15 无边框强制执行的影响。

这是一项重大变更,可能会对应用的界面产生负面影响。这些更改会影响以下界面区域:

  • 手势手柄导航栏
    • 默认为透明。
    • 底部偏移已停用,因此除非应用边衬区,否则内容会绘制在系统导航栏后面。
    • setNavigationBarColorR.attr#navigationBarColor 已废弃,不会影响手势导航。
    • setNavigationBarContrastEnforcedR.attr#navigationBarContrastEnforced 仍然对手势导航没有影响。
  • “三按钮”导航
    • 不透明度默认设置为 80%,颜色可能与窗口背景匹配。
    • 已停用底部偏移,因此内容绘制在系统导航栏后面,除非应用边衬区。
    • 默认情况下,setNavigationBarColorR.attr#navigationBarColor 设置为与窗口背景匹配。窗口背景必须是彩色可绘制对象,才能应用此默认设置。此 API 已废弃,但会继续影响“三按钮”导航。
    • setNavigationBarContrastEnforcedR.attr#navigationBarContrastEnforced 默认情况下为 true,这会在“三按钮”导航中添加 80% 的不透明背景。
  • 状态栏
    • 默认为透明。
    • 顶部偏移量已停用,因此,除非应用边衬区,否则内容会显示在状态栏后面。
    • setStatusBarColorR.attr#statusBarColor 已废弃,对 Android 15 没有任何影响。
    • setStatusBarContrastEnforcedR.attr#statusBarContrastEnforced 已废弃,但在 Android 15 上仍然有效。
  • 刘海屏
    • 非浮动窗口的 layoutInDisplayCutoutMode 必须为 LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYSSHORT_EDGESNEVERDEFAULT 会被解释为 ALWAYS,这样用户就不会看到由刘海屏导致的黑条,而无边框。

以下示例展示了以 Android 15 为目标平台之前和之后以及应用边衬区前后的应用。

应用以 Android 14 为目标平台且在 Android 15 设备上并非全屏显示的应用。
一个以 Android 15 为目标平台且在 Android 15 设备上实现无边框的应用。不过,由于 Android 15 的无边框强制措施,许多元素现在被状态栏、“三按钮”导航栏或刘海屏遮挡。被遮盖的界面包括 Material 2 顶部应用栏、悬浮操作按钮和列表项。
如果应用以 Android 15 为目标平台,则可以在 Android 15 设备上全屏显示,并会应用边衬区来避免界面被遮挡。
检查应用是否已无边框的检查方法

如果您的应用已经无边框并应用了边衬区,则基本上不受影响,下列场景除外。不过,即使您认为自己不受影响,我们也建议您测试自己的应用。

  • 您有一个非浮动窗口,例如,使用 SHORT_EDGESNEVERDEFAULT(而非 LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS)的 Activity。如果您的应用在启动时崩溃,这可能是由于启动画面所致。您可以将核心启动画面依赖项升级到 1.2.0-alpha01 或更高版本,也可以设置 window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutInDisplayCutoutMode.always
  • 可能存在流量较低且界面被遮挡的屏幕。验证这些访问较少的屏幕没有被遮挡的界面。流量较低的屏幕包括:
    • 初始配置或登录屏幕
    • “设置”页面
如果您的应用还不是无边框应用的,请检查哪些内容

如果您的应用尚未边缘化,那么您很有可能受到影响。除了已实现无边框应用的场景之外,您还应考虑以下事项:

  • 如果应用在 Compose 中使用 Material 3 组件 (androidx.compose.material3)(例如 TopAppBarBottomAppBarNavigationBar),这些组件可能不会受到影响,因为它们会自动处理边衬区。
  • 如果应用在 Compose 中使用 Material 2 组件 (androidx.compose.material),这些组件不会自动处理边衬区。不过,您可以访问边衬区并手动应用边衬区。在 androidx.compose.material 1.6.0 及更高版本中,使用 windowInsets 参数为 BottomAppBarTopAppBarBottomNavigationNavigationRail 手动应用边衬区。同样,请为 Scaffold 使用 contentWindowInsets 参数。
  • 如果您的应用使用视图和 Material 组件 (com.google.android.material),大多数基于视图的 Material 组件(例如 BottomNavigationViewBottomAppBarNavigationRailViewNavigationView)会处理边衬区,无需额外的工作。不过,如果使用 AppBarLayout,则需要添加 android:fitsSystemWindows="true"
  • 对于自定义可组合项,请手动应用边衬区作为内边距。如果您的内容位于 Scaffold 内,您可以使用 Scaffold 内边距值消耗边衬区。否则,请使用 WindowInsets 之一应用内边距。
  • 如果您的应用使用的是视图和 BottomSheetSideSheet 或自定义容器,请使用 ViewCompat.setOnApplyWindowInsetsListener 应用内边距。对于 RecyclerView,请使用此监听器应用内边距,并添加 clipToPadding="false"
检查应用是否必须提供自定义后台保护的事项

如果您的应用必须为“三按钮”导航或状态栏提供自定义后台保护,则应使用 WindowInsets.Type#tappableElement() 获取“三按钮”导航栏高度或 WindowInsets.Type#statusBars,将可组合项或视图放置在系统栏后面。

其他无边框资源

如需了解应用边衬区的其他注意事项,请参阅边缘视图Edge to Edge Compose 指南。

已弃用的 API

以下 API 现已废弃:

稳定配置

如果您的应用以 Android 15 或更高版本为目标平台,Configuration 将不再排除系统栏。如果您在 Configuration 类中使用屏幕尺寸计算布局,则应根据需要将其替换为合适的 ViewGroupWindowInsetsWindowMetricsCalculator 等更好的替代方案。

从 API 1 开始,Configuration 一直可用。它通常从 Activity.onConfigurationChanged 获取。它可提供窗口密度、方向和大小等信息。从 Configuration 返回的窗口大小的一个重要特征是它之前排除了系统栏。

配置大小通常用于资源选择(如 /res/layout-h500dp),这仍然是一个有效的用例。不过,我们一直不建议将其用于布局计算。如果这样做,您应该立即离开。您应该根据自己的使用场景,将 Configuration 替换为更合适的代码。

如果您使用其计算布局,请使用适当的 ViewGroup,例如 CoordinatorLayoutConstraintLayout。如果您使用它来确定系统导航栏的高度,请使用 WindowInsets。如果您想知道应用窗口的当前大小,请使用 computeCurrentWindowMetrics

以下列表介绍了受此更改影响的字段:

EnglishTextHeight 属性默认为 true

For apps targeting Android 15, the elegantTextHeight TextView attribute becomes true by default, replacing the compact font used by default with some scripts that have large vertical metrics with one that is much more readable. The compact font was introduced to prevent breaking layouts; Android 13 (API level 33) prevents many of these breakages by allowing the text layout to stretch the vertical height utilizing the fallbackLineSpacing attribute.

In Android 15, the compact font still remains in the system, so your app can set elegantTextHeight to false to get the same behavior as before, but it is unlikely to be supported in upcoming releases. So, if your app supports the following scripts: Arabic, Lao, Myanmar, Tamil, Gujarati, Kannada, Malayalam, Odia, Telugu or Thai, test your app by setting elegantTextHeight to true.

elegantTextHeight behavior for apps targeting Android 14 (API level 34) and lower.
elegantTextHeight behavior for apps targeting Android 15.

复杂字母形状的 TextView 宽度变化

在以前的 Android 版本中,一些采用复杂形状的草体字体或语言可能会在上一个或下一个字符区域中绘制字母。在某些情况下,此类字母会在开始或结束位置被截断。从 Android 15 开始,TextView 会分配宽度来为此类字母绘制足够的空间,并允许应用请求左侧添加额外的内边距以防止被裁剪。

由于此变更会影响 TextView 确定宽度的方式,因此如果应用以 Android 15 或更高版本为目标平台,TextView 会默认分配更多宽度。您可以通过对 TextView 调用 setUseBoundsForWidth API 来启用或停用此行为。

由于添加左侧内边距可能会导致现有布局未对齐,因此默认情况下,系统不会添加内边距,即使对于以 Android 15 或更高版本为目标平台的应用也是如此。不过,您可以通过调用 setShiftDrawingOffsetForStartOverhang 添加额外的内边距以防止裁剪。

以下示例展示了这些更改如何改进某些字体和语言的文本布局。

以手写体字体显示的英语文本的标准布局。部分字母会被截断。相应的 XML 如下:

<TextView
    android:fontFamily="cursive"
    android:text="java" />
包含额外宽度和内边距的相同英文文本的布局。相应的 XML 如下:

<TextView
    android:fontFamily="cursive"
    android:text="java"
    android:useBoundsForWidth="true"
    android:shiftDrawingOffsetForStartOverhang="true" />
泰语文字的标准版式。部分字母被截断。 相应的 XML 如下:

<TextView
    android:text="คอมพิวเตอร์" />
相同泰语文本的布局,但有额外的宽度和内边距。相应的 XML 如下:

<TextView
    android:text="คอมพิวเตอร์"
    android:useBoundsForWidth="true"
    android:shiftDrawingOffsetForStartOverhang="true" />

EditText 的语言区域感知型默认行高

In previous versions of Android, the text layout stretched the height of the text to meet the line height of the font that matched the current locale. For example, if the content was in Japanese, because the line height of the Japanese font is slightly larger than the one of a Latin font, the height of the text became slightly larger. However, despite these differences in line heights, the EditText element was sized uniformly, regardless of the locale being used, as illustrated in the following image:

Three boxes representing EditText elements that can contain text from English (en), Japanese (ja), and Burmese (my). The height of the EditText is the same, even though these languages have different line heights from each other.

For apps targeting Android 15, a minimum line height is now reserved for EditText to match the reference font for the specified Locale, as shown in the following image:

Three boxes representing EditText elements that can contain text from English (en), Japanese (ja), and Burmese (my). The height of the EditText now includes space to accommodate the default line height for these languages' fonts.

If needed, your app can restore the previous behavior by specifying the useLocalePreferredLineHeightForMinimum attribute to false, and your app can set custom minimum vertical metrics using the setMinimumFontMetrics API in Kotlin and Java.

摄像头和媒体

对于以 Android 15 或更高版本为目标平台的应用,Android 15 对相机和媒体行为做出了以下变更。

请求音频焦点的限制

Apps that target Android 15 must be the top app or running an audio-related foreground service in order to request audio focus. If an app attempts to request focus when it does not meet one of these requirements, the call returns AUDIOFOCUS_REQUEST_FAILED.

A foreground service is considered audio-related if its type is mediaPlayback, camera, microphone, or phoneCall.

You can learn more about audio focus at Manage audio focus.

更新后的非 SDK 限制

Android 15 includes updated lists of restricted non-SDK interfaces based on collaboration with Android developers and the latest internal testing. Whenever possible, we make sure that public alternatives are available before we restrict non-SDK interfaces.

If your app does not target Android 15, some of these changes might not immediately affect you. However, while it's possible for your app to access some non-SDK interfaces depending on your app's target API level, using any non-SDK method or field always carries a high risk of breaking your app.

If you are unsure if your app uses non-SDK interfaces, you can test your app to find out. If your app relies on non-SDK interfaces, you should begin planning a migration to SDK alternatives. Nevertheless, we understand that some apps have valid use cases for using non-SDK interfaces. If you can't find an alternative to using a non-SDK interface for a feature in your app, you should request a new public API.

如需详细了解此 Android 版本中的变更,请参阅 Android 15 中有关限制非 SDK 接口的更新。如需全面了解有关非 SDK 接口的详细信息,请参阅对非 SDK 接口的限制