程式碼範例
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
請使用下列程式碼範例,瞭解 Android 8.0 (API 級別 26) 的功能和 API。如要在 Android Studio 中下載範例,請依序選取「File」>「New」>「Import Samples」選單選項。
注意:這些可下載的專案專為搭配 Gradle 和 Android Studio 使用而設計。
自動填入架構
AutoFillFramework 範例:這個範例示範如何使用 Android 8.0 (API 級別 26) 中推出的 AutoFillFramework。這包括要自動填入的用戶端活動實作項目,以及可為這些活動提供自動填入資料的服務。
前往 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 範例:示範如何在應用程式活動不可見時,使用前景服務取得位置更新。對於在 Android 8.0 (API 級別 26) 上執行的應用程式,背景更新次數上限為每小時數次。使用前景服務是接收更頻繁更新的一種方式。
前往 GitHub 取得:
Java
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間: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"]],["上次更新時間: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)"]]