Tổng quan về các tính năng và API

Android 14 mang đến cho nhà phát triển các tính năng và API mới tuyệt vời. Các phần sau đây giúp bạn tìm hiểu các tính năng cho ứng dụng cũng như làm quen với các API liên quan.

Để biết danh sách chi tiết về các API đã thêm, đã sửa đổi và đã xoá, hãy đọc báo cáo điểm khác biệt về API. Để biết thông tin chi tiết về các API đã thêm, hãy truy cập vào tài liệu tham khảo về API cho Android. Đối với Android 14, hãy tìm các API đã được thêm vào API cấp 34. Để tìm hiểu những thay đổi của nền tảng có thể tác động đến ứng dụng của bạn, hãy nhớ tham khảo các thay đổi về hành vi của Android 14 đối với ứng dụng nhắm đến Android 14tất cả ứng dụng.

Quốc tế hoá

Lựa chọn ưu tiên về ngôn ngữ cho mỗi ứng dụng

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) 的应用语言可见性:IME 可以利用 getApplicationLocales() 方法查看当前应用的语言,并将 IME 语言与该语言进行匹配。

API Biến tố ngữ pháp

有 30 亿人在使用区分性别的语言,此类语言的语法类别(例如名词、动词、形容词和介词)会根据您交谈所涉及的人或物的性别而变化。传统上,许多区分性别的语言使用阳性语法性别作为默认或通用性别。

以错误的语法性别来称呼用户,例如以阳性语法性别来称呼女性,可能会对她们的表现和态度产生负面影响。相比之下,界面语言如果能正确反映用户的语法性别,就可以提高用户互动度,并提供更个性化、更自然的用户体验。

为帮助您针对区分性别的语言构建以用户为中心的界面,Android 14 引入了 Grammatical Inflection API,让您无需重构应用便能添加对语法性别的支持。

Lựa chọn ưu tiên theo khu vực

Regional preferences enable users to personalize temperature units, the first day of the week, and numbering systems. A European living in the United States might prefer temperature units to be in Celsius rather than Fahrenheit and for apps to treat Monday as the beginning of the week instead of the US default of Sunday.

New Android Settings menus for these preferences provide users with a discoverable and centralized location to change app preferences. These preferences also persist through backup and restore. Several APIs and intents—such as getTemperatureUnit and getFirstDayOfWeek— grant your app read access to user preferences, so your app can adjust how it displays information. You can also register a BroadcastReceiver on ACTION_LOCALE_CHANGED to handle locale configuration changes when regional preferences change.

To find these settings, open the Settings app and navigate to System > Languages & input > Regional preferences.

Regional preferences screen in Android system settings.
Temperature options for regional preferences in Android system settings.

Hỗ trợ tiếp cận

Điều chỉnh tỷ lệ phông chữ phi tuyến tính lên đến 200%

Starting in Android 14, the system supports font scaling up to 200%, providing low-vision users with additional accessibility options that align with Web Content Accessibility Guidelines (WCAG).

To prevent large text elements on screen from scaling too large, the system applies a nonlinear scaling curve. This scaling strategy means that large text doesn't scale at the same rate as smaller text. Nonlinear font scaling helps preserve the proportional hierarchy between elements of different sizes while mitigating issues with linear text scaling at high degrees (such as text being cut off or text that becomes harder to read due to an extremely large display sizes).

Test your app with nonlinear font scaling

Enable the maximum font size in a device's accessibility settings to test your app.

If you already use scaled pixels (sp) units to define text sizing, then these additional options and scaling improvements are applied automatically to the text in your app. However, you should still perform UI testing with the maximum font size enabled (200%) to ensure that your app applies the font sizes correctly and can accommodate larger font sizes without impacting usability.

To enable 200% font size, follow these steps:

  1. Open the Settings app and navigate to Accessibility > Display size and text.
  2. For the Font size option, tap the plus (+) icon until the maximum font size setting is enabled, as shown in the image that accompanies this section.

Use scaled pixel (sp) units for text-sizes

Remember to always specify text sizes in sp units. When your app uses sp units, Android can apply the user's preferred text size and scale it appropriately.

Don't use sp units for padding or define view heights assuming implicit padding: with nonlinear font scaling sp dimensions might not be proportional, so 4sp + 20sp might not equal 24sp.

Convert scaled pixel (sp) units

Use TypedValue.applyDimension() to convert from sp units to pixels, and use TypedValue.deriveDimension() to convert pixels to sp. These methods apply the appropriate nonlinear scaling curve automatically.

Avoid hardcoding equations using Configuration.fontScale or DisplayMetrics.scaledDensity. Because font scaling is nonlinear, the scaledDensity field is no longer accurate. The fontScale field should be used for informational purposes only because fonts are no longer scaled with a single scalar value.

Use sp units for lineHeight

Always define android:lineHeight using sp units instead of dp, so the line height scales along with your text. Otherwise, if your text is sp but your lineHeight is in dp or px, it doesn't scale and looks cramped. TextView automatically corrects the lineHeight so that your intended proportions are preserved, but only if both textSize and lineHeight are defined in sp units.

Camera và nội dung nghe nhìn

Ultra HDR cho hình ảnh

标准动态范围 (SDR) 与高动态范围 (HDR) 图片质量对比示意图。

Android 14 新增了对高动态范围 (HDR) 图片的支持,可在拍摄照片时保留更多来自传感器的信息,从而实现鲜艳的色彩和更高的对比度。Android 使用 Ultra HDR 格式,该格式与 JPEG 图片完全向后兼容,可让应用与 HDR 图片无缝互操作,并根据需要以标准动态范围 (SDR) 显示这些图片。

当您的应用选择为其 activity 窗口使用 HDR 界面(通过清单条目或通过在运行时调用 Window.setColorMode())时,框架会自动在界面中以 HDR 格式渲染这些图片。您还可以在受支持的设备上拍摄压缩的 Ultra HDR 静态图片。从传感器中恢复的颜色越多,后期编辑的灵活性就越高。与 Ultra HDR 图片关联的 Gainmap 可用于使用 OpenGL 或 Vulkan 渲染这些图片。

Thu phóng, Lấy nét, Xem nhanh và nhiều tính năng khác trong tiện ích máy ảnh

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

Thu phóng trong cảm biến

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 目标搭配使用。通过实现请求替换控件,更新后的相机可让用户在其他相机控件准备就绪之前使用缩放控件。

Âm thanh qua cổng USB không bị suy hao

Android 14 支持无损音频格式,可通过 USB 有线耳机提供发烧友级体验。您可以查询 USB 设备的首选混音器属性,注册监听器以监听首选混音器属性的更改,以及使用 AudioMixerAttributes 类配置混音器属性。此类表示音频混音器的格式,例如声道掩码、采样率和行为。该类允许直接发送音频,而无需混音、调节音量或处理效果。

Năng suất và công cụ dành cho nhà phát triển

Trình quản lý thông tin xác thực

Android 14 将 Credential Manager 添加为平台 API,并通过使用 Google Play 服务的 Jetpack 库,向后额外支持 Android 4.4(API 级别 19)设备。Credential Manager 旨在通过 API 使用用户配置的凭据提供程序检索和存储凭据,让用户更轻松地登录。Credential Manager 在单个 API 中支持多种登录方法,包括用户名和密码、通行密钥和联合登录解决方案(如“使用 Google 账号登录”)。

通行密钥具有许多优势。例如,通行密钥是基于业界标准构建的,可在各种不同的操作系统和浏览器生态系统中使用,并且可用于网站和应用。

如需了解详情,请参阅 Credential Manager 和通行密钥文档以及介绍 Credential Manager 和通行密钥的博文

Health Connect

Health Connect là một kho lưu trữ trên thiết bị dành cho dữ liệu sức khoẻ và thể chất của người dùng. Tính năng này cho phép người dùng chia sẻ dữ liệu giữa các ứng dụng mà họ yêu thích, với một nơi duy nhất để kiểm soát dữ liệu họ muốn chia sẻ với các ứng dụng này.

Trên các thiết bị chạy phiên bản Android trước Android 14, bạn có thể tải Health Connect xuống dưới dạng ứng dụng trên Cửa hàng Google Play. Kể từ Android 14, Health Connect là một phần của nền tảng và nhận được bản cập nhật thông qua bản cập nhật hệ thống của Google Play mà không cần tải xuống riêng. Nhờ đó, Health Connect có thể được cập nhật thường xuyên và các ứng dụng của bạn có thể dựa vào Health Connect có sẵn trên các thiết bị chạy Android 14 trở lên. Người dùng có thể truy cập vào Health Connect từ phần Cài đặt trong thiết bị của họ, với các chế độ kiểm soát quyền riêng tư được tích hợp vào phần cài đặt hệ thống.

Người dùng có thể bắt đầu sử dụng Health Connect mà không cần tải ứng dụng riêng xuống trên các thiết bị chạy Android 14 trở lên.
Người dùng có thể kiểm soát những ứng dụng có quyền truy cập vào dữ liệu sức khoẻ và thể chất của họ thông qua phần cài đặt hệ thống.

Health Connect có một số tính năng mới trong Android 14, chẳng hạn như tuyến đường tập thể dục, cho phép người dùng chia sẻ tuyến đường tập thể dục của họ và có thể được trực quan hoá trên bản đồ. Tuyến đường được xác định là danh sách các vị trí được lưu trong một khoảng thời gian và ứng dụng của bạn có thể chèn các tuyến đường vào các phiên tập thể dục, liên kết các tuyến đường đó với nhau. Để đảm bảo người dùng có toàn quyền kiểm soát dữ liệu nhạy cảm này, người dùng phải cho phép chia sẻ từng tuyến đường với các ứng dụng khác.

Để biết thêm thông tin, hãy xem tài liệu về Health Connect và bài đăng trên blog về Tính năng mới trong Android Health.

Nội dung cập nhật 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.

Những điểm cải tiến cho cửa hàng ứng dụng

Android 14 giới thiệu một số API PackageInstaller cho phép cửa hàng ứng dụng cải thiện trải nghiệm người dùng.

Yêu cầu phê duyệt lượt cài đặt trước khi tải xuống

Có thể bạn phải yêu cầu người dùng phê duyệt việc cài đặt hoặc cập nhật ứng dụng. Ví dụ: khi trình cài đặt sử dụng quyền REQUEST_INSTALL_PACKAGES cố gắng cài đặt một ứng dụng mới. Trong các phiên bản Android trước, cửa hàng ứng dụng chỉ có thể yêu cầu người dùng phê duyệt sau khi các tệp APK được ghi vào phiên cài đặt còn phiên hoạt động thì được cam kết.

Kể từ Android 14, phương thức requestUserPreapproval() sẽ cho phép trình cài đặt yêu cầu người dùng phê duyệt trước khi xác nhận phiên cài đặt. Điểm cải tiến này cho phép cửa hàng ứng dụng trì hoãn việc tải mọi tệp APK xuống cho đến khi người dùng phê duyệt quá trình cài đặt. Hơn nữa, sau khi người dùng phê duyệt việc cài đặt, cửa hàng ứng dụng có thể tải và cài đặt ứng dụng ở chế độ nền mà không làm gián đoạn người dùng.

Tuyên bố trách nhiệm đối với các bản cập nhật trong tương lai

Phương thức setRequestUpdateOwnership() cho phép trình cài đặt cho hệ thống biết rằng sau này trình cài đặt dự định sẽ chịu trách nhiệm về các bản cập nhật của ứng dụng đang cài đặt. Tính năng này cho phép thực thi theo quyền sở hữu bản cập nhật, nghĩa là chỉ chủ sở hữu bản cập nhật mới được phép tự động cài đặt bản cập nhật cho ứng dụng. Việc thực thi theo quyền sở hữu bản cập nhật giúp đảm bảo rằng người dùng chỉ nhận được bản cập nhật từ cửa hàng ứng dụng theo đúng mong đợi.

Mọi trình cài đặt khác (bao gồm cả những trình cài đặt sử dụng quyền INSTALL_PACKAGES) phải nhận được sự chấp thuận rõ ràng của người dùng để có thể cài đặt bản cập nhật. Nếu người dùng quyết định tiếp tục cập nhật từ một nguồn khác, thì quyền sở hữu bản cập nhật sẽ bị mất.

Cập nhật ứng dụng vào những thời điểm ít gây gián đoạn hơn

Các cửa hàng ứng dụng thường muốn tránh cập nhật một ứng dụng đang được sử dụng, vì việc này dẫn đến việc các quy trình đang chạy của ứng dụng bị dừng, có khả năng làm gián đoạn công việc mà người dùng đang thực hiện.

Kể từ Android 14, InstallConstraints API sẽ giúp trình cài đặt có thể đảm bảo quá trình cập nhật ứng dụng diễn ra đúng lúc. Ví dụ: một cửa hàng ứng dụng có thể gọi phương thức commitSessionAfterInstallConstraintsAreMet() để đảm bảo rằng bản cập nhật chỉ được cam kết khi người dùng không còn tương tác với ứng dụng có liên quan.

Cài đặt các phần phân tách không bắt buộc sao cho liền mạch

Nhờ tệp APK phân tách, nhiều tính năng của một ứng dụng có thể được phân phối trong các tệp APK riêng biệt, thay vì dưới dạng tệp APK nguyên khối. Tệp APK phân tách cho phép cửa hàng ứng dụng tối ưu hoá việc phân phối nhiều thành phần của ứng dụng. Ví dụ: có thể các cửa hàng ứng dụng sẽ tối ưu hoá dựa trên thuộc tính của thiết bị mục tiêu. PackageInstaller API đã hỗ trợ các phần phân tách kể từ khi ra mắt trong API cấp 22.

Trong Android 14, phương thức setDontKillApp() sẽ cho phép trình cài đặt cho biết rằng các quy trình đang chạy của ứng dụng sẽ không bị tắt khi các phần phân tách mới được cài đặt. Các cửa hàng ứng dụng có thể sử dụng tính năng này để cài đặt liền mạch những tính năng mới của một ứng dụng trong khi người dùng đang sử dụng ứng dụng đó.

Gói siêu dữ liệu ứng dụng

Starting in Android 14, the Android package installer lets you specify app metadata, such as data safety practices, to include on app store pages such as Google Play.

Phát hiện thời điểm người dùng chụp ảnh màn hình thiết bị

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.

Trải nghiệm người dùng

Các thao tác tuỳ chỉnh trên Trang chia sẻ nội dung và cách cải thiện thứ hạng

Android 14 updates the system sharesheet to support custom app actions and more informative preview results for users.

Add custom actions

With Android 14, your app can add custom actions to the system sharesheet it invokes.

Screenshot of custom actions on the sharesheet.

Improve ranking of Direct Share targets

Android 14 uses more signals from apps to determine the ranking of the direct share targets to provide more helpful results for the user. To provide the most useful signal for ranking, follow the guidance for improving rankings of your Direct Share targets. Communication apps can also report shortcut usage for outgoing and incoming messages.

Direct Share row in the sharesheet, as shown by 1

Hỗ trợ ảnh động tích hợp sẵn và ảnh động tuỳ chỉnh cho tính năng Xem trước thao tác quay lại

视频:预测性返回动画

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

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

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

Chế độ ghi đè cho mỗi ứng dụng của nhà sản xuất thiết bị có màn hình lớn

借助按应用替换项,设备制造商可以更改应用在大屏设备上的行为。例如,FORCE_RESIZE_APP 替换项会指示系统调整应用大小以适应显示屏尺寸(避免进入尺寸兼容模式),即使在应用清单中设置了 resizeableActivity="false" 也是如此。

替换项旨在改善大屏设备上的用户体验。

借助新的清单属性,您可以为应用停用某些设备制造商替换项。

Chế độ ghi đè cho mỗi ứng dụng của người dùng trên màn hình lớn

按应用替换项会更改应用在大屏设备上的行为。例如,无论应用的配置如何,OVERRIDE_MIN_ASPECT_RATIO_LARGE 设备制造商替换项都会将应用宽高比设置为 16:9。

借助 Android 14 QPR1,用户可以在大屏设备上通过新的设置菜单应用按应用替换项。

Chia sẻ màn hình ứng dụng

借助应用界面共享功能,用户可以在录制屏幕内容时共享应用窗口,而不是整个设备屏幕。

在应用屏幕共享模式下,状态栏、导航栏、通知和其他系统界面元素会从共享显示屏中排除。系统只会分享所选应用的内容。

应用屏幕共享功能可让用户运行多个应用,但将内容共享限制为单个应用,从而提高工作效率并保护隐私。

Tính năng Trả lời thông minh dựa trên LLM trong Gboard trên Pixel 8 Pro

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.

Đồ hoạ

Các đường dẫn nay truy vấn được và nội suy được

Android 的 Path API 是一种强大且灵活的机制,可用于创建和渲染矢量图形,能够描边或填充路径、根据线段或二次曲线或立方曲线构建路径、执行布尔运算以获取更复杂的形状,或同时执行所有这些操作。但有一个限制是,您无法了解 Path 对象中实际包含的内容;该对象的内部信息在创建后对调用方是不透明的。

如需创建 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。

Lưới tuỳ chỉnh có chương trình đổ bóng đỉnh và mảnh

Android 长期以来一直支持使用自定义着色绘制三角网格,但输入网格格式仅限于一些预定义的属性组合。Android 14 增加了对自定义网格的支持,可将其定义为三角形三角形条,并且可以选择是否编入索引。这些网格是使用自定义属性、顶点步长、可变以及使用 AGSL 编写的顶点着色器和片段着色器指定的。

顶点着色器定义了位置和颜色等变量,而片段着色器可以选择为像素定义颜色,通常是使用顶点着色器创建的变量。如果片段着色器提供颜色,则系统会使用绘制网格时选择的混合模式将其与当前 Paint 颜色混合。Uniform 可以传递到片段着色器和顶点着色器,以提高灵活性。

Trình kết xuất vùng đệm phần cứng cho Canvas

协助使用 Android 的 Canvas API 通过 硬件加速至 HardwareBuffer、Android 14 引入了 HardwareBufferRenderer。如果您的用例涉及通过 SurfaceControl 与系统合成器通信以实现低延迟绘制,此 API 特别有用。