功能與 API 總覽

Android 14 為開發人員推出了強大的新功能和 API。以下資訊可協助您瞭解應用程式功能,並開始使用相關的 API。

如需新增、修改及移除 API 的詳細清單,請參閱 API 差異比較表。如要進一步瞭解新增的 API,請參閱 Android API 參考資料。如果是 Android 14,請找出 API 級別 34 中新增的 API。如要瞭解平台變更可能對應用程式造成的影響,請務必查看指定 Android 14 的應用程式所有應用程式 的 Android 14 行為變更。

國際化

個別應用程式語言偏好

Android 14 進一步延伸 Android 13 (API 級別 33) 所推出的個別應用程式語言功能,並新增多項功能:

  • 自動產生應用程式的 localeConfig:自 Android Studio Giraffe Canary 7 和 AGP 8.1.0-alpha07 起,您可以將應用程式設為自動支援個別應用程式語言偏好設定。Android Gradle 外掛程式會根據您的專案資源產生 LocaleConfig 檔案,並在最終資訊清單檔案中為該檔案新增參照,為您省去手動建立或更新檔案的需求。AGP 會使用應用程式模組 res 資料夾中的資源和任何程式庫模組依附元件,以決定要納入 LocaleConfig 檔案的語言代碼。

  • 應用程式 localeConfig 的動態更新:請使用 法LocaleManager 中的 setOverrideLocaleConfig()getOverrideLocaleConfig() 方法,以動態方式更新應用程式在裝置的系統設定中的支援語言清單。如果應用程式使用伺服器端推送進行本地化,則可利用此彈性自訂各區域的支援語言清單、執行 A/B 實驗,或提供更新的語言代碼清單。

  • 輸入法編輯器 (IME) 的應用程式語言瀏覽權限:輸入法編輯器可透過 getApplicationLocales() 方法檢查當前應用程式的語言,並和輸入法編輯器的語言進行比對。

文法轉變 API

30 億人使用性別化語言:即名詞、動詞、形容詞和介系詞會隨著交談或談論對象或目標的性別,而有不同變化形式的文法類別語言。一般來說,許多性別化語言都會使用陽性文法性別做為預設或通用性別。

若以錯誤的文法性別稱呼使用者,例如:以陽性文法性別稱呼女性,可能會對其表現及態度產生負面影響。相較之下,如果使用者介面的用語正確反映了使用者的文法性別,便可提高使用者參與度,同時提供更個人化且更自然親切的使用者體驗。

To help you build a user-centric UI for gendered languages, Android 14 introduces the Grammatical Inflection API, which lets you add support for grammatical gender without refactoring your app.

地區偏好設定

用户可通过地区偏好设置对温度单位、一周的第一天和编号系统进行个性化设置。居住在美国的欧洲用户可能更希望使用摄氏度,而不是华氏度,并且希望应用将星期一视为一周的开始,而不是像美国那样默认从星期日开始。

新 Android 设置菜单包含这些偏好设置,使用户能够在一个位置集中发现这些应用更改偏好设置。这些偏好设置在备份和恢复设备后也会保持不变。多个 API 和 intent(例如 getTemperatureUnitgetFirstDayOfWeek)会为您的应用授予读取权限来访问用户偏好设置,因此您的应用可以调整其显示信息的方式。您还可以在 ACTION_LOCALE_CHANGED 上注册 BroadcastReceiver,以便在地区偏好设置发生更改时处理语言区域配置更改。

如需找到这些设置,请打开“设置”应用,然后依次前往系统 > 语言和输入法 > 地区偏好设置

Android 系统设置中的地区偏好设置界面。
Android 系统中地区偏好设置的温度选项 设置。

無障礙設定

非線性字型縮放至 200%

自 Android 14 起,系統將支援高達 200% 的字型縮放功能,為低視能使用者提供符合無障礙網頁內容規範 (WCAG)的額外無障礙選項。

為避免畫面上的大型文字元素縮放過大,系統會套用非線性縮放曲線。套用此縮放策略後,大型文字的縮放比例會和小型文字的縮放比例有所區別。非線性字型縮放有助於保留不同大小元素之間的比例階層,同時減少高度線性文字縮放的問題 (例如文字遭到截斷,或因顯示大小過大而難以閱讀的文字)。

使用非線性字型縮放測試應用程式

請在裝置的無障礙設定中啟用字型大小上限,以便測試您的應用程式。

如果您已使用縮放像素 (sp) 單位來定義文字大小,這些額外選項和縮放改善功能會自動套用至應用程式中的文字。不過,您仍應執行 UI 測試,並將字型大小上限設為啟用 (200%),以確保應用程式能正確套用字型大小,而且能在不影響可用性的情況下容納較大的字型大小。

如要啟用 200% 的字型大小,請按照下列步驟進行:

  1. 開啟「設定」應用程式,然後依序前往「無障礙中心」>「顯示大小與文字」
  2. 在「字型大小」選項中,輕觸加號 (+) 圖示,直到達字型大小上限為止,如本節隨附的圖片所示。

使用經過調整像素 (sp) 的單位處理文字大小

請記得一律以 sp 單位指定文字大小。如果您的應用程式使用 sp 單位,Android 可套用使用者偏好的文字大小,並適當縮放。

請勿使用 sp 單位做為邊框間距,或是假定檢視畫面高度,並假設有隱性邊框間距:使用非線性字型縮放 sp 尺寸可能不是比例,因此 4sp + 20sp 不一定會等於 24sp。

轉換經過調整像素 (sp) 的單位

使用 TypedValue.applyDimension() 將 sp 單位轉換為像素,並使用 TypedValue.deriveDimension() 將像素轉換為 sp。這些方法會自動套用適當的非線性縮放曲線。

避免使用硬式編碼方程式使用 Configuration.fontScaleDisplayMetrics.scaledDensity。由於字型縮放功能為非線性方式,因此 scaledDensity 欄位已變得不準確。fontScale 欄位應僅用於提供資訊,因為字型不再需要以單一純量值縮放字型。

使用 sp 單位做為 lineHeight

請一律使用 sp 單位 (而非 dp) 定義 android:lineHeight,讓行高會隨著文字縮放。否則,如果文字是 sp,但 lineHeight 是以 dp 或 px 為單位,則無法縮放,看起來會遭到破壞。TextView 會自動修正 lineHeight,以便保留預期的比例,但前提是 textSizelineHeight 都已在 sp 單位中定義。

相機和媒體

圖片的 Ultra HDR

An illustration of Standard Dynamic Range (SDR) versus High Dynamic Range (HDR) image quality.

Android 14 adds support for High Dynamic Range (HDR) images that retain more of the information from the sensor when taking a photo, which enables vibrant colors and greater contrast. Android uses the Ultra HDR format, which is fully backward compatible with JPEG images, allowing apps to seamlessly interoperate with HDR images, displaying them in Standard Dynamic Range (SDR) as needed.

Rendering these images in the UI in HDR is done automatically by the framework when your app opts in to using HDR UI for its Activity Window, either through a manifest entry or at runtime by calling Window.setColorMode(). You can also capture compressed Ultra HDR still images on supported devices. With more colors recovered from the sensor, editing in post can be more flexible. The Gainmap associated with Ultra HDR images can be used to render them using OpenGL or Vulkan.

相機擴充功能中的縮放、聚焦、後視等功能

Android 14 升级并改进了相机扩展程序,让应用能够处理更长的处理时间,从而支持在受支持的设备上使用计算密集型算法(例如弱光摄影)来改善图片。这些功能可让用户在使用相机扩展功能时获得更出色的体验。这些改进的示例包括:

感應器內縮放

CameraCharacteristics 中的 REQUEST_AVAILABLE_CAPABILITIES_STREAM_USE_CASE 包含 SCALER_AVAILABLE_STREAM_USE_CASES_CROPPED_RAW 时,您的应用可以使用高级传感器功能,将剪裁后的 RAW 数据流的像素与全视野范围相同,方法是将 CaptureRequest 与将数据流用例设置为 CameraMetadata.SCALER_AVAILABLE_STREAM_USE_CASES_CROPPED_RAW 的 RAW 目标搭配使用。通过实现请求替换控件,更新后的相机可让用户在其他相机控件准备就绪之前使用缩放控件。

無損 USB 音訊

Android 14 支援無損音訊格式,可透過 USB 有線耳機提供頂級音響體驗。您可以查詢 USB 裝置的偏好混合器屬性、註冊偏好混合器屬性變更的事件監聽器,以及使用 AudioMixerAttributes 類別設定混合器屬性。這個類別代表音訊調音器的格式,例如通道遮罩、取樣率和行為。這個類別可讓您直接傳送音訊,不必進行混合、音量調整或處理效果。

開發人員工作效率和工具

Credential Manager

Android 14 會新增 Credential Manager 做為平台 API,並透過使用 Google Play 服務的 Jetpack 程式庫,進一步支援 Android 4.4 (API 級別 19) 裝置。Credential Manager 的目標是透過 API 協助使用者輕鬆登入,這些 API 會透過使用者設定的憑證提供者擷取及儲存憑證。Credential Manager 可在單一 API 中支援多種登入方式,包括使用者名稱和密碼、密碼金鑰,以及聯合登入解決方案 (例如使用 Google 帳戶登入)。

密碼金鑰具有許多優點,舉例來說,密碼金鑰採用業界標準,可在不同作業系統和瀏覽器環境中運作,也能與網站和應用程式搭配使用。

如需更多資訊,請參閱 Credential Manager 和密碼金鑰說明文件,以及Credential Manager 和密碼金鑰相關網誌文章

Health Connect

Health Connect is an on-device repository for user health and fitness data. It allows users to share data between their favorite apps, with a single place to control what data they want to share with these apps.

On devices running Android versions prior to Android 14, Health Connect is available to download as an app on the Google Play store. Starting with Android 14, Health Connect is part of the platform and receives updates through Google Play system updates without requiring a separate download. With this, Health Connect can be updated frequently, and your apps can rely on Health Connect being available on devices running Android 14 or higher. Users can access Health Connect from the Settings in their device, with privacy controls integrated into the system settings.

Users can get started using Health Connect without a separate app download on devices running Android 14 or higher.
Users can control which apps have access to their health and fitness data through system settings.

Health Connect includes several new features in Android 14, such as exercise routes, allowing users to share a route of their workout which can be visualized on a map. A route is defined as a list of locations saved within a window of time, and your app can insert routes into exercise sessions, tying them together. To ensure that users have complete control over this sensitive data, users must allow sharing individual routes with other apps.

For more information, see the Health Connection documentation and the blogpost on What's new in Android Health.

OpenJDK 17 更新

Android 14 continues the work of refreshing Android's core libraries to align with the features in the latest OpenJDK LTS releases, including both library updates and Java 17 language support for app and platform developers.

The following features and improvements are included:

  • Updated approximately 300 java.base classes to Java 17 support.
  • Text Blocks, which introduce multi-line string literals to the Java programming language.
  • Pattern Matching for instanceof, which allows an object to be treated as having a specific type in an instanceof without any additional variables.
  • Sealed classes, which allow you restrict which classes and interfaces can extend or implement them.

Thanks to Google Play system updates (Project Mainline), over 600 million devices are enabled to receive the latest Android Runtime (ART) updates that include these changes. This is part of our commitment to give apps a more consistent, secure environment across devices, and to deliver new features and capabilities to users independent of platform releases.

Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

應用程式商店改善項目

Android 14 引入了多个 PackageInstaller API,可帮助应用商店改善其用户体验。

下载之前请求批准安装

安装或更新应用可能需要用户批准。例如,当使用 REQUEST_INSTALL_PACKAGES 权限的安装程序尝试安装新应用时。在之前的 Android 版本中,只有在将 APK 写入安装会话并且提交会话后,应用商店才能请求用户批准。

从 Android 14 开始,requestUserPreapproval() 方法可让安装程序在提交安装会话之前请求用户批准。此项改进可让应用商店将任何 APK 的下载操作推迟到用户批准安装之后。此外,用户批准安装后,应用商店可以在后台下载并安装应用,而不会干扰用户。

承担未来更新的责任

借助 setRequestUpdateOwnership() 方法,安装程序可以向系统表明它打算负责将被安装的应用未来的更新。此 capability 可实现更新所有权强制执行,即仅允许更新所有者为应用安装自动更新。更新所有权强制执行有助于确保用户仅收到来自预期应用商店的更新。

任何其他安装程序(包括使用 INSTALL_PACKAGES 权限的安装程序)都必须获得用户的明确批准,才能安装更新。如果用户决定继续从其他来源安装更新,则会失去更新所有权。

在干扰较少的时段更新应用

应用商店通常希望避免更新正在使用的应用,因为这会导致应用正在运行的进程被终止,而这可能会中断用户正在执行的操作。

从 Android 14 开始,InstallConstraints API 让安装程序可以确保其应用更新在适当的时机进行。例如,应用商店可以调用 commitSessionAfterInstallConstraintsAreMet() 方法来确保仅在用户不再与相应应用互动时才进行更新。

无缝安装可选拆分

借助拆分 APK,应用的功能可以通过单独的 APK 文件提供,而不是以单体式 APK 的形式提供。借助拆分 APK,应用商店可以优化不同应用组件的提供。例如,应用商店可能会根据目标设备的属性进行优化。自在 API 级别 22 中引入以来,PackageInstaller API 一直支持拆分。

在 Android 14 中,setDontKillApp() 方法可让安装程序指明在安装新的拆分项时应用的运行进程不应终止。应用商店可以使用此功能,在用户使用应用时无缝安装应用的新功能。

應用程式中繼資料套件

從 Android 14 開始,Android 套件安裝程式可讓您指定應用程式中繼資料 (例如資料安全性做法),以加入 Google Play 等應用程式商店頁面。

在使用者擷取裝置螢幕畫面時偵測

To create a more standardized experience for detecting screenshots, Android 14 introduces a privacy-preserving screenshot detection API. This API lets apps register callbacks on a per-activity basis. These callbacks are invoked, and the user is notified, when the user takes a screenshot while that activity is visible.

使用者體驗

Sharesheet 自訂動作與改善排名

Android 14 更新了系统 Sharesheet,以便为用户提供自定义应用操作和信息更丰富的预览结果。

添加自定义操作

对于 Android 14,您的应用可以向其调用的系统 Sharesheet 添加自定义操作

分享表格中自定义操作的屏幕截图。

提高直接共享目标的排名

Android 14 根据来自应用的更多信号来确定直接共享目标的排名,以便为用户提供更实用的结果。为了提供最实用的排名信号,请遵循提高直接共享目标排名的准则。通讯应用还可以报告出站和入站消息的快捷方式使用情况

共享表单中的“直接分享”行,如 1
所示

支援預測返回功能的內建和自訂動畫

视频:预测性返回动画

Android 13 在开发者选项背后引入了预测性“返回主屏幕”动画。在已启用开发者选项的受支持应用中使用时,滑回手势会显示动画,表明返回手势会使应用退回到主屏幕。

Android 14 包含针对“预测性返回”的多项改进和新指南:

在此 Android 14 预览版中,所有预测性返回功能都是位于开发者选项背后。请参阅与将您的应用迁移到预测性返回有关的开发者指南,以及与创建自定义应用内转换有关的开发者指南

大螢幕裝置製造商個別應用程式覆寫值

個別應用程式覆寫值可讓裝置製造商變更大螢幕裝置上的應用程式行為。舉例來說,即使應用程式資訊清單中已設定 resizeableActivity="false"FORCE_RESIZE_APP 覆寫值仍會指示系統根據螢幕尺寸調整應用程式大小 (避免大小相容性模式)。

覆寫設定的用意是改善大螢幕的使用者體驗。

新的資訊清單屬性可讓您為應用程式停用部分裝置製造商覆寫值。

大螢幕使用者個別應用程式覆寫值

個別應用程式覆寫值可變更應用程式在大螢幕裝置上的行為。舉例來說,無論應用程式設定為何,OVERRIDE_MIN_ASPECT_RATIO_LARGE 裝置製造商覆寫值都會將應用程式顯示比例設為 16:9。

在 Android 14 QPR1 中,使用者可透過新的設定選單,在大螢幕裝置上套用個別應用程式覆寫值。

應用程式分享螢幕畫面

分享應用程式畫面功能可讓使用者在錄製螢幕內容時,分享應用程式視窗,而非整個裝置畫面。

透過應用程式螢幕分享功能,系統會從共用畫面中排除狀態列、導覽列、通知和其他系統 UI 元素。只會分享所選應用程式的內容。

應用程式螢幕分享功能可讓使用者執行多個應用程式,但只分享單一應用程式的內容,進而提升工作效率和隱私權。

在 Pixel 8 Pro 上使用 LLM 技術支援的 Gboard 智慧回覆功能

On Pixel 8 Pro devices with the December Feature Drop, developers can try out higher-quality smart replies in Gboard powered by on-device Large Language Models (LLMs) running on Google Tensor.

This feature is available as a limited preview for US English in WhatsApp, Line, and KakaoTalk. It requires using a Pixel 8 Pro device with Gboard as your keyboard.

To try it out, first enable the feature in Settings > Developer Options > AiCore Settings > Enable Aicore Persistent.

Next, open a conversation in a supported app to see LLM-powered Smart Reply in Gboard's suggestion strip in response to incoming messages.

Gboard utilizes on-device LLMs to provide higher-quality smart replies.

圖形

路徑可供查詢及內插

Android 的 Path API 是一種強大且靈活的機制,可用於建立和算繪向量圖形,並具有描邊或填入路徑、從線段或二次方或三次方曲線建構路徑、執行布林運算以取得更複雜的形狀,或同時執行所有這些操作的功能。但有一個限制,就是無法找出路徑物件中的實際內容;物件的內部部分在建立後,無法明確顯示於呼叫端。

如要建立 Path,您必須呼叫 moveTo()lineTo()cubicTo() 等方法,以便新增路徑區隔。不過,您無法透過對該路徑提出詢問以瞭解確切的區隔,因此必須在建立時保留該資訊。

自 Android 14 起,您便可查詢路徑,找出其中的內容。首先,您需要使用 Path.getPathIterator API 取得 PathIterator 物件:

Kotlin

val path = Path().apply {
    moveTo(1.0f, 1.0f)
    lineTo(2.0f, 2.0f)
    close()
}
val pathIterator = path.pathIterator

Java

Path path = new Path();
path.moveTo(1.0F, 1.0F);
path.lineTo(2.0F, 2.0F);
path.close();
PathIterator pathIterator = path.getPathIterator();

接下來,您可以呼叫 PathIterator 逐一疊代區隔,並擷取每個區隔的所有必要資料。本範例使用封裝資料的 PathIterator.Segment 物件:

Kotlin

for (segment in pathIterator) {
    println("segment: ${segment.verb}, ${segment.points}")
}

Java

while (pathIterator.hasNext()) {
    PathIterator.Segment segment = pathIterator.next();
    Log.i(LOG_TAG, "segment: " + segment.getVerb() + ", " + segment.getPoints());
}

PathIterator 也提供 next() 的非分配式版本,可供您傳入緩衝區,以便保留單點資料。

內插類型是查詢 Path 資料的重要用途之一。例如,您可能想要在兩個不同路徑之間建立動畫 (或 變形)。為進一步簡化該使用情境,Android 14 也在 Path 中加入 interpolate() 方法。假設兩個路徑具有相同的內部結構,interpolate() 方法會使用該內插結果建立新的 Path。此範例會傳回形狀介於 pathotherPath 之間半行 (0 .5 的線性內插) 的路徑:

Kotlin

val interpolatedResult = Path()
if (path.isInterpolatable(otherPath)) {
    path.interpolate(otherPath, .5f, interpolatedResult)
}

Java

Path interpolatedResult = new Path();
if (path.isInterpolatable(otherPath)) {
    path.interpolate(otherPath, 0.5F, interpolatedResult);
}

Jetpack graphics-path 程式庫也會為舊版 Android 啟用類似的 API。

使用頂點和片段著色器的自訂網格

Android has long supported drawing triangle meshes with custom shading, but the input mesh format has been limited to a few predefined attribute combinations. Android 14 adds support for custom meshes, which can be defined as triangles or triangle strips, and can, optionally, be indexed. These meshes are specified with custom attributes, vertex strides, varying, and vertex and fragment shaders written in AGSL.

The vertex shader defines the varyings, such as position and color, while the fragment shader can optionally define the color for the pixel, typically by using the varyings created by the vertex shader. If color is provided by the fragment shader, it is then blended with the current Paint color using the blend mode selected when drawing the mesh. Uniforms can be passed into the fragment and vertex shaders for additional flexibility.

Canvas 的硬體緩衝區轉譯器

為協助使用 Android 的 Canvas API 以硬體加速繪製至 HardwareBuffer,Android 14 推出了 HardwareBufferRenderer。如果您的用途涉及透過 SurfaceControl 與系統合成器通訊,以便繪製低延遲畫面,這個 API 就特別實用。