代码示例
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
通过下面的代码示例,了解 Android 8.0(API 级别 26)的功能和 API。如需在 Android Studio 中下载这些示例,请依次选择 File > New > Import Samples 菜单选项。
注意:这些可下载的项目旨在与 Gradle 和 Android Studio 配合使用。
自动填充框架
AutofillFramework 示例 - 此示例演示了如何使用 Android 8.0(API 级别 26)中引入的自动填充框架。其中包括希望获得自动填充的客户端 activity 的实现,以及可为这些 activity 提供自动填充数据的服务。
在 GitHub 中获取:
Java |
Kotlin
画中画模式
PictureInPicture 示例 - 此示例演示了“画中画”模式在手持设备上的基本使用方法。此示例会播放一段视频。应用进入“画中画”模式后,该视频会继续播放。在“画中画”屏幕中,应用会显示可暂停或恢复视频播放的操作项。
在 GitHub 中获取:
Java |
Kotlin
可下载字体
DownloadableFonts - 此示例演示了如何使用 Android 8.0(API 级别 26)中引入的“可下载字体”功能。借助“可下载字体”功能,应用可向提供商请求特定字体,而无需打包或亲自下载相应字体。这意味着,不再需要单独将字体打包为资源。
在 GitHub 中获取:
Java |
Kotlin
EmojiCompat - 此示例演示了如何使用表情符号兼容性支持库。您可以使用此库来防止应用以豆腐块形式 (□) 显示缺失的表情符号。您既可以使用打包的表情符号字体,也可以使用可下载的表情符号字体。此示例展示了上述两种用法。
在 GitHub 中获取:
Java |
Kotlin
后台执行限制
蓝牙广告示例 - 更新了蓝牙广告示例,以便符合 Android 8.0(API 级别 26)的后台执行限制要求。该示例之前创建了一项后台服务,用于广播蓝牙 LE 广告;此流程现在作为前台服务启动,以确保可以执行。
在 GitHub 中获取:
Java
后台位置限制
LocationUpdatesPendingIntent 示例 - 此示例展示了如何使用 PendingIntent
请求位置信息更新。对于以 Android 7.x(API 级别 24-25)为目标平台、但在 Android 8.0(API 级别 26)上运行的应用,开发者可使用 PendingIntent.getService()
或 PendingIntent.getBroadcast()
。对于以 Android 8.0 为目标平台的应用,由于对在后台启动的服务施加了限制,因此 PendingIntent.getService()
无法正常工作。如果应用以 Android 8.0 为目标平台,则开发者应使用 PendingIntent.getBroadcast()
。
在 GitHub 中获取:
Java
LocationUpdatesForegroundService 示例 - 此示例展示了如何在应用 activity 不可见时使用前台服务获取位置信息更新。对于在 Android 8.0(API 级别 26)上运行的应用,后台更新每小时仅进行几次。使用前台服务可接收更频繁的更新。
在 GitHub 中获取:
Java
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Code Samples\n\nUse the code samples below to learn about Android 8.0 (API level 26)\ncapabilities and APIs. To download the samples in Android Studio,\nselect the **File \\\u003e New \\\u003e Import Samples** menu option.\n\n\n**Note:** These downloadable projects are designed\nfor use with Gradle and Android Studio.\n\nAutofill Framework\n------------------\n\n**AutofillFramework Sample** -\nThis sample demonstrates the use of the Autofill Framework introduced in\nAndroid 8.0 (API level 26). It includes implementations of client\nActivities that want to be autofilled, and a Service that can provide\nautofill data to those Activities.\n\n\nGet it on GitHub: [Java](https://github.com/android/input-samples/tree/main/AutofillFramework) \\| [Kotlin](https://github.com/android/input-samples/tree/main/AutofillFrameworkKotlin)\n\nPicture-in-Picture Mode\n-----------------------\n\n**PictureInPicture Sample** -\nThis sample demonstrates basic usage of Picture-in-Picture mode for handheld\ndevices. The sample plays a video. The video keeps on playing when the app is\nturned into Picture-in-Picture mode. On Picture-in-Picture screen, the app\nshows an action item to pause or resume the video.\n\n\nGet it on GitHub: [Java](https://github.com/android/media-samples/tree/main/PictureInPicture/) \\| [Kotlin](https://github.com/android/media-samples/tree/main/PictureInPictureKotlin)\n\nDownloadable Fonts\n------------------\n\n**DownloadableFonts** -\nThis sample demonstrates how to use the Downloadable Fonts feature introduced\nin Android 8.0 (API level 26). Downloadable Fonts allows apps to request a\ncertain font from a provider, instead of bundling it or downloading it\nthemselves. This means there is no need to independently bundle the font\nas an asset.\n\n\nGet it on GitHub: [Java](https://github.com/android/user-interface-samples/tree/main/DownloadableFonts/) \\| [Kotlin](https://github.com/android/user-interface-samples/tree/main/DownloadableFontsKotlin)\n\n**EmojiCompat** -\nThis sample demonstrates usage of the Emoji Compatibility Support Library. You\ncan use this library to prevent your app from showing missing emoji characters\nin the form of tofu (□). You can use either bundled or downloadable emoji\nfonts. This sample shows both usages.\n\n\nGet it on GitHub: [Java](https://github.com/android/user-interface-samples/tree/main/EmojiCompat) \\| [Kotlin](https://github.com/android/user-interface-samples/tree/main/EmojiCompatKotlin)\n\nBackground Execution Limits\n---------------------------\n\n**Bluetooth Advertisements Sample** -\nThe Bluetooth Advertisements sample was updated to comply with Android 8.0\n(API level 26)'s background execution limits. The sample previously\ncreated a background service which was used to broadcast Bluetooth LE\nAdvertisements; this process is now started as a foreground service\nto ensure execution.\n\n\nGet it on GitHub: [Java](https://github.com/android/connectivity-samples/tree/main/BluetoothAdvertisements)\n\nBackground Location Restrictions\n--------------------------------\n\n**LocationUpdatesPendingIntent Sample** -\nShows how to request location updates using a `PendingIntent`. For\napps targeting Android 7.x (API levels 24-25) but running on Android 8.0\n(API level 26), developers can use either\n`PendingIntent.getService()` or\n`PendingIntent.getBroadcast()`. For apps targeting Android 8.0,\n`PendingIntent.getService()` does not work due to the limits\nplaced on services started in the background. When targeting Android 8.0,\ndevelopers should use `PendingIntent.getBroadcast()`.\n\n\nGet it on GitHub: [Java](https://github.com/android/location-samples/tree/432d3b72b8c058f220416958b444274ddd186abd/LocationUpdatesPendingIntent)\n\n**LocationUpdatesForegroundService Sample** -\nShows how to use a foreground service to get location updates when the app\nactivities are not visible. For apps running on Android 8.0 (API level 26),\nbackground updates are limited to\nonly a few times per hour. Using a foreground service is a way to receive more\nfrequent updates.\n\n\nGet it on GitHub: [Java](https://github.com/android/location-samples/blob/432d3b72b8c058f220416958b444274ddd186abd/LocationUpdatesForegroundService)"]]