功能和 API 概览

Android 15 面向开发者引入了一些出色的新功能和 API。以下部分总结了这些功能,以帮助您开始使用相关 API。

如需查看新增、修改和移除的 API 的详细列表,请参阅 API 差异报告。如需详细了解新的 API,请访问 Android API 参考文档,新 API 会突出显示以方便查看。此外,如需了解平台变更可能会在哪些方面影响您的应用,请务必查看 Android 15 以 Android 15 为目标平台的应用对应用有影响的行为变更以及影响所有应用(无论 targetSdkVersion 如何)的行为变更

摄像头和媒体

Android 15 包含各种功能,可改善相机和媒体体验,并可让您使用各种工具和硬件来支持创作者在 Android 上将其构想变为现实。

应用内相机控件

Android 15 添加了一个新扩展,用于在支持的设备上更好地控制相机硬件及其算法:

  • 高级闪光灯强度调整,可让您在拍摄照片时精确控制 SINGLETORCH 模式下的闪光强度。

HDR 余量控制

Android 15 chooses HDR headroom that is appropriate for the underlying device capabilities and bit-depth of the panel. For pages that have lots of SDR content, such as a messaging app displaying a single HDR thumbnail, this behavior can end up adversely influencing the perceived brightness of the SDR content. Android 15 lets you control the HDR headroom with setDesiredHdrHeadroom to strike a balance between SDR and HDR content.

The brightness of SDR UI elements on the left screen appears to be more uniform than the brightness on the right screen, which simulates possible headroom issues when HDR and SDR content are mixed. By adjusting the HDR headroom, you can achieve a better balance between the SDR and HDR content.

音量控制

Android 15 引入了对 CTA-2075 音量标准的支持,以帮助您避免音频音量不一致,并确保用户在切换内容时无需不断调整音量。系统利用输出设备(耳机和扬声器)的已知特征以及 AAC 音频内容中提供的音量元数据来智能地调整音频音量和动态范围压缩级别。

如需启用此功能,您需要确保 AAC 内容中有音量元数据,并在您的应用中启用平台功能。为此,您可以使用关联的 AudioTrack 中的音频会话 ID 调用其 create 工厂方法,从而实例化此对象;此方法会自动开始应用音频更新。LoudnessCodecController您可以传递 OnLoudnessCodecUpdateListener 以在将音量参数应用于 MediaCodec 之前修改或过滤这些参数。

// Media contains metadata of type MPEG_4 OR MPEG_D
val mediaCodec = …
val audioTrack = AudioTrack.Builder()
                                .setSessionId(sessionId)
                                .build()
...
// Create new loudness controller that applies the parameters to the MediaCodec
try {
   val lcController = LoudnessCodecController.create(mSessionId)
   // Starts applying audio updates for each added MediaCodec

AndroidX media3 ExoPlayer 即将更新,以便利用 LoudnessCodecController API 实现无缝应用集成。

弱光增强

Android 15 引入了低光增强,这是一种新的自动曝光模式,可供 Camera 2夜间模式相机扩展使用。“低光增强”功能可调整预览视频流在弱光条件下的曝光量。这与夜间模式相机扩展程序创建静态图片的方式不同,因为夜间模式会组合多张照片来制作单张增强图片。虽然夜间模式非常适合创建静态图片,但无法创建连续的帧流,但“低光增强”可以。因此,“低光增强”可以实现新的相机功能,例如:

  • 提供增强的图片预览,以便用户更好地为弱光环境取景
  • 在光线昏暗的环境下扫描二维码

如果您启用“弱光增强”,它会在光线较暗时自动开启,在光线较暗时关闭。

应用可以在光线昏暗的环境下录制预览流,以保存明亮的视频。

有关详情,请参阅弱光增强

虚拟 MIDI 2.0 设备

Android 13 添加了对使用 USB 连接到 MIDI 2.0 设备的支持,USB 使用通用 MIDI 数据包 (UMP) 进行通信。Android 15 扩展了对虚拟 MIDI 应用的 UMP 支持,使合成器应用能够像对待 USB MIDI 2.0 设备一样将合成器应用作为虚拟 MIDI 2.0 设备进行控制。

畅连乐享

Android 15 更新了该平台,让您的应用能够获享通信领域的最新进展。

卫星支持

Android 15 continues to extend platform support for satellite connectivity and includes some UI elements to ensure a consistent user experience across the satellite connectivity landscape.

Apps can use ServiceState.isUsingNonTerrestrialNetwork() to detect when a device is connected to a satellite, giving them more awareness of why full network services might be unavailable. Additionally, Android 15 provides support for SMS and MMS apps as well as preloaded RCS apps to use satellite connectivity for sending and receiving messages.

A notification appears when the device connects to a satellite.

更顺畅的 NFC 体验

Android 15 正努力让感应式付款体验更加顺畅和可靠,同时继续支持 Android 强大的 NFC 应用生态系统。在受支持的设备上,应用可以请求 NfcAdapter 进入观察模式,在该模式下,设备会监听 NFC 读取器,但不响应 NFC 读取器,同时发送应用的 NFC 服务 PollingFrame 对象进行处理。PollingFrame 对象可用于在首次与 NFC 读取器通信之前进行身份验证,在许多情况下可实现一键交易。

开发者工作效率和工具

虽然我们的大部分办公工作都围绕 Android StudioJetpack ComposeAndroid Jetpack 库等工具展开,但我们始终在平台中寻找方法来帮助您更轻松地实现愿景。

改进了 PDF

Android 15 开发者预览版 2 包含对 PdfRenderer API 重大改进的早期预览版。应用可以整合各种高级功能,例如渲染受密码保护的文件、注解、表单编辑搜索和通过文案进行选择。支持线性 PDF 优化,以加快本地 PDF 的查看速度并减少资源使用量。

针对 PDF 渲染功能的最新更新包括搜索嵌入式 PDF 文件等功能。

PdfRenderer 已移至一个模块,此模块可使用与平台版本无关的 Google Play 系统更新来进行更新,并且我们将通过创建兼容的 API Surface 低于 Android 15 的版本(称为 PdfRendererPreV)来支持在 Android 11(API 级别 30)中更新这些变更。

我们非常重视您对 PdfRenderer API 接口的增强功能的反馈,我们计划通过即将推出的 Android Jetpack 库,让开发者能更轻松地将这些 API 整合到您的应用中。

语言切换自动优化

Android 14 增加了设备端音频中的多语言识别功能,可在语言之间自动切换,但这可能会导致单词丢失,尤其是在语言切换时,两种话语之间的停顿较少时。Android 15 添加了额外的控件,可帮助应用根据其用例调整此切换。EXTRA_LANGUAGE_SWITCH_INITIAL_ACTIVE_DURATION_TIME_MILLIS 会将自动切换限制为仅在音频会话开始时进行,而 EXTRA_LANGUAGE_SWITCH_MATCH_SWITCHES 则会在指定次数的切换后停用语言切换。如果您希望自动检测到会话期间只讲一种语言,这些选项特别有用。

细化的换行符控件

从 Android 15 开始,TextView 和底层换行符可以将文本的给定部分保留在同一行中,以提高可读性。您可以通过在字符串资源或 createNoBreakSpan 中使用 <nobreak> 标记来利用此换行符自定义功能。同样,您可以使用 <nohyphen> 标记或 createNoHyphenationSpan 避免断字的情况。

例如,以下字符串资源不包含换行符,而是在不希望的位置显示文本“Pixel 8 Pro”进行渲染:

<resources>
    <string name="pixel8pro">The power and brains behind Pixel 8 Pro.</string>
</resources>

相比之下,此字符串资源包含 <nobreak> 标记,该标记会封装短语“Pixel 8 Pro.”并防止换行符:

<resources>
    <string name="pixel8pro">The power and brains behind <nobreak>Pixel 8 Pro.</nobreak></string>
</resources>

这些字符串的呈现方式差异如下图所示:

用于设置一行文本的布局,其中词组“Pixel 8 Pro”未使用 <nobreak> 标记进行换行。
同一行文本的布局,其中词组“Pixel 8 Pro”用 <nobreak> 标记封装。

OpenJDK 17 更新

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

其中包含以下主要功能和改进:

这些 API 通过 Google Play 系统更新在超过 10 亿台搭载 Android 12(API 级别 31)及更高版本的设备上进行了更新,让您能够以最新的编程功能为目标平台。

Java 和 OpenJDK 是 Oracle 及/或其关联公司的商标或注册商标。

大屏设备和外形规格

Android 15 支持您的应用充分利用 Android 设备类型,包括大屏设备、可翻转设备和可折叠设备。

封面屏幕支持

您的应用可以声明一项属性,Android 15 会使用该属性将您的 ApplicationActivity 呈现在支持的可翻转设备的小封面屏幕上。这些屏幕太小,无法被视为 Android 应用的兼容目标,但您的应用可以选择支持这些屏幕,从而让您的应用可在更多位置使用。

性能和电池

Android 将继续专注于帮助您提高应用的性能和质量。Android 15 引入了新的 API,可帮助您更高效地执行应用中的任务、优化应用性能,以及收集有关应用的数据洞见。

ApplicationStartInfo API

在以前的 Android 版本中,应用启动有点神秘。在应用中确定应用是从冷状态、温状态还是热状态开始的过程很难确定。此外,我们也很难知道您的应用在不同发布阶段所用的时间:创建进程分支、调用 onCreate、绘制第一帧等。在实例化 Application 类时,您无法得知应用是从广播、content provider、作业、备份、启动完成、闹钟还是 Activity 启动的。

Android 15 上的 ApplicationStartInfo API 可提供所有这些功能以及更多其他功能。您甚至可以选择将自己的时间戳添加到流程中,以便在一个位置收集时间数据。除了收集指标之外,您还可以使用 ApplicationStartInfo 直接优化应用启动;例如,当应用因广播而启动时,您可以避免在 Application 类中实例化界面相关库的高成本。

详细的应用大小信息

自 Android 8.0(API 级别 26)起,Android 就一直包含 StorageStats.getAppBytes API,该 API 将应用的安装大小汇总为一个字节,这些字节是 APK 大小、从 APK 中提取的文件的大小以及设备上生成的文件(例如预先 (AOT) 编译代码)的总和。就应用的存储空间使用情况而言,此数字并不富有见解。

Android 15 增加了 StorageStats.getAppBytesByDataType([type]) API,可让您深入了解应用如何使用所有空间,包括 APK 文件拆分、AOT 和加速相关代码、dex 元数据、库和引导式配置文件。

SQLite 数据库改进

Android 15 introduces new SQLite APIs that expose advanced features from the underlying SQLite engine that target specific performance issues that can manifest in apps.

Developers should consult best practices for SQLite performance to get the most out of their SQLite database, especially when working with large databases or when running latency-sensitive queries.

  • Read-only deferred transactions: when issuing transactions that are read-only (don't include write statements), use beginTransactionReadOnly() and beginTransactionWithListenerReadOnly(SQLiteTransactionListener) to issue read-only DEFERRED transactions. Such transactions can run concurrently with each other, and if the database is in WAL mode, they can run concurrently with IMMEDIATE or EXCLUSIVE transactions.
  • Row counts and IDs: new APIs were added to retrieve the count of changed rows or the last inserted row ID without issuing an additional query. getLastChangedRowCount() returns the number of rows that were inserted, updated, or deleted by the most recent SQL statement within the current transaction, while getTotalChangedRowCount() returns the count on the current connection. getLastInsertRowId() returns the rowid of the last row to be inserted on the current connection.
  • Raw statements: issue a raw SQlite statement, bypassing convenience wrappers and any additional processing overhead that they may incur.

Android 动态性能框架更新

Android 15 继续投资开发 Android 动态性能框架 (ADPF),这是一组 API,可让游戏和性能密集型应用更直接地与 Android 设备的电源和散热系统进行互动。在受支持的设备上,Android 15 将添加新的 ADPF 功能:

  • 适用于提示会话的节能模式,用于指明其关联的线程应该优先考虑节能而非性能,非常适合长时间运行的后台工作负载。
  • 可以在提示会话中报告 GPU 和 CPU 的运行时长,从而使系统能够同时调整 CPU 和 GPU 频率,以便更好地满足工作负载需求。
  • 热余量阈值,用于根据余量预测来解释可能的热节流状态。

如需详细了解如何在应用和游戏中使用 ADPF,请参阅相关文档

隐私权

Android 15 包含各种有助于应用开发者保护用户隐私的功能。

屏幕录制检测

Android 15 adds support for apps to detect that they are being recorded. A callback is invoked whenever the app transitions between being visible or invisible within a screen recording. An app is considered visible if activities owned by the registering process's UID are being recorded. This way, if your app is performing a sensitive operation, you can inform the user that they're being recorded.

val mCallback = Consumer<Int> { state ->
  if (state == SCREEN_RECORDING_STATE_VISIBLE) {
    // We're being recorded
  } else {
    // We're not being recorded
  }
}

override fun onStart() {
   super.onStart()
   val initialState =
      windowManager.addScreenRecordingCallback(mainExecutor, mCallback)
   mCallback.accept(initialState)
}

override fun onStop() {
    super.onStop()
    windowManager.removeScreenRecordingCallback(mCallback)
}

扩展的 IntentFilter 功能

Android 15 构建了通过 UriRelativeFilterGroup 支持更精确的 Intent 解析,其中包含一组 UriRelativeFilter 对象,这些对象构成了一组必须满足的 Intent 匹配规则,其中包括网址查询参数、网址片段以及屏蔽或排除规则。

您可以使用新的 <uri-relative-filter-group> 标记在 AndroidManifest XML 文件中定义这些规则,也可以选择包含 android:allow 标记。这些标记可以包含使用现有数据标记属性以及新的 android:queryandroid:fragment 属性的 <data> 标记。

下面是 AndroidManifest 语法的示例:

<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="http" />
  <data android:scheme="https" />
  <data android:domain="astore.com" />
  <uri-relative-filter-group>
    <data android:pathPrefix="/auth" />
    <data android:query="region=na" />
  </uri-relative-filter-group>
  <uri-relative-filter-group android:allow="false">
    <data android:pathPrefix="/auth" />
    <data android:query="mobileoptout=true" />
  </uri-relative-filter-group>
  <uri-relative-filter-group android:allow="false">
    <data android:pathPrefix="/auth" />
    <data android:fragmentPrefix="faq" />
  </uri-relative-filter-group>
</intent-filter>

Privacy Sandbox on Android

Android 15 将 Android 广告服务提升到扩展级别 10,在其中融入了最新版本的 Privacy Sandbox on Android。我们致力于开发新技术来加强用户隐私保护,并为移动应用提供有效的个性化广告体验。我们的隐私沙盒页面详细介绍了 Privacy Sandbox on Android 开发者预览版和 Beta 版计划,以帮助您开始使用。

Health Connect

Android 15 围绕 Health Connect by Android 集成了 Android 14 扩展程序 10。Health Connect by Android 是一个安全的集中式平台,用于管理和共享应用收集的健康与健身数据。此更新增加了对健身营养等领域的新数据类型的支持。

局部屏幕共享

Android 15 支持局部屏幕共享,因此用户可以仅分享或录制应用窗口,而不是整个设备屏幕。此功能在 Android 14 QPR2 中首次启用,包含 MediaProjection 回调,可让您的应用自定义局部屏幕共享体验。请注意,对于以 Android 14(API 级别 34)或更高版本为目标平台的应用,现在需要针对每个 MediaProjection 捕获会话征得用户同意

安全性

Android 15 可帮助您增强应用的安全性并保护应用的数据。

使用 fs-verity 保护文件

Android 15 的 FileIntegrityManager 包含一些可让您利用 Linux 内核中 fs-verity 功能的强大功能的新 API。借助 fs-verity,您可以通过自定义加密签名保护文件,从而确保文件未被篡改或损坏。这有助于增强安全性,防止潜在的恶意软件或未经授权的文件修改,从而损害应用的功能或数据。

用户体验

Android 15 为应用开发者和用户提供了更大的控制力和灵活性,让他们能够根据自己的需求配置设备。

改进了“勿扰”规则

AutomaticZenRule 可让应用自定义注意力管理(勿扰)规则,并决定何时启用或停用这些规则。Android 15 大幅增强了这些规则,以改善用户体验。其中包含以下增强功能:

  • 将类型添加到 AutomaticZenRule,允许系统对某些规则应用特殊处理。
  • AutomaticZenRule 添加了图标,有助于使模式更易于识别。
  • AutomaticZenRule 添加 triggerDescription 字符串,用于描述应在哪些条件下为用户启用规则。
  • AutomaticZenRule 添加了 ZenDeviceEffects,以允许规则触发灰度显示、夜间模式或调暗壁纸等功能。