遷移至 Google Play 服務的位置和內容 API
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
針對需要定位服務的應用程式,Google 建議在 Google Play 服務中使用位置與內容 API。如果應用程式使用架構位置 API,請務必遷移至 Google Play 服務,以便善用 Google 提供的最新功能。
建議選擇使用 Google Play 服務,在應用程式中取得定位服務,原因如下:
- Google Play 服務提供簡單的介面和更加簡潔的 API 途徑。
- 您指定所需的服務品質,然後 API 會為您管理基礎技術。
- Google Play 服務 API 已針對效能和電池用量進行最佳化。
- Google Play 服務 API 會主動維護。Google 會持續改進演算法,並新增更多功能。
請更新應用程式
下列步驟說明更新應用程式的過程,以便使用位置與內容 API:
- 在專案中設定 Google Play 服務。
- 使用位置資訊設定 API 驗證目前的位置資訊設定。
- 使用 Google Play 服務提供地理圍欄、活動辨識和位置辨識等複雜功能。
- 從使用架構位置 API 變成使用整合式位置預測提供工具 API。
- 移除架構位置 API 的參照。
在專案中設定 Google Play 服務
若要為專案提供位置與內容 API,需要新增 Google Maven 存放區的參照,並宣告必要 API 的依附元件。詳情請參閱「設定 Google Play 服務」一文。
使用位置資訊設定 API
只要使用位置資訊設定 API,應用程式就會提供所需的 QoS 級別,而且 API 會要求使用者適當變更系統設定。請依下列步驟操作,在應用程式中使用位置資訊設定 API:
- 在應用程式資訊清單中要求位置存取權。
- 設定
LocationRequest
物件,指定所需的 QoS 層級。
- 使用位置資訊設定 API 檢查目前的設定。
詳情請參閱「變更位置資訊設定」,或參閱 Google Play 位置範例,取得範例程式碼。
使用 Google Play 服務提供複雜功能
借助 Google Play 服務程式庫,您可以依據使用者周遭的情境和位置辨識情形,導入創新的使用者體驗。我們的位置和情境程式庫種類繁多,不僅可善用位置以外的其他感應器,還能以省電的方式達到相同目的,為應用程式提供更精確的資料,同時減少對電池續航力的影響。
建議您在應用程式中整合這些程式庫,而不要自行編寫自訂解決方案:
將架構 location API 替換為整合式位置預測提供工具 API
您可使用整合式位置預測提供工具 API 來取得位置資料,例如經緯度。整合式位置預測提供工具 API 會使用 Location
物件 (如同位置架構 API) 表示地理位置。這個 API 的功能包括監聽位置更新,以及取得最後已知位置。多虧這些功能,整合式位置預測提供工具 API 十分適合替代使用架構位置 API 的元件,對應用程式其餘部分的變更也最少。
對許多體驗來說,取得最後已知位置功能是很好的切入點,因為這是一項快速作業,能使用裝置上任何用戶端要求的位置資料。如要定期追蹤位置,應用程式可以訂閱接收位置更新,藉此提供最新資料,並支援更複雜的操作體驗。
移除架構 location API 的參照
請將 com.google.android.location
套件中的類別參照替換為 com.google.android.gms.location
套件中的類別,但整合式位置預測提供工具 API 使用的 Location
類別參照除外。一般來說,您可以從應用程式中移除管理不同提供工具 (例如 GPS 和 Wi-Fi) 的元件。位置與內容 API 會自動管理這些提供工具。
測試應用程式
如要執行使用最新版 Google Play 服務的應用程式,須備有已安裝 Play 商店應用程式的裝置,並且需要登入 Google 帳戶。在開發方面,您可以使用下列選項:
- 透過 USB 傳輸線連接至開發環境的實體裝置。
- 已安裝 Play 商店應用程式的模擬器。
若要進一步瞭解如何將實體裝置連接至開發環境,請參閱在硬體裝置上執行應用程式。若要建立包含 Play 商店應用程式的模擬器,請參閱建立和管理虛擬裝置。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Migrate to Google Play services location and context APIs\n\nGoogle recommends using the [location and context\nAPIs](https://developers.google.com/location-context/) in Google Play services\nin apps that require location services. If your app uses the [framework location\nAPIs](/reference/android/location/package-summary) it's important to\nmigrate to Google Play services to take advantage of the latest Google-powered\nfeatures.\n\nUsing Google Play services is the preferred option to get location services in\nyour app for the following reasons:\n\n1. Google Play services provide a simple interface and a cleaner API surface.\n2. You specify a desired quality of service and the APIs manage the underlying technologies for you.\n3. The Google Play services APIs are optimized for performance and battery usage.\n4. The Google Play services APIs are actively maintained. Google is constantly improving the algorithms and adding more features.\n\nUpdate your app\n---------------\n\nThe following steps describe the process to update an app to use the location\nand context APIs:\n\n1. Set up Google Play services in your project.\n2. Use the location settings API to validate the current location settings.\n3. Use Google Play services for complex features like geofencing, activity recognition, and awareness.\n4. Replace usage of the framework location API with the fused location provider API.\n5. Remove references to the framework location API.\n\n### Set up Google Play services in your project\n\nTo make the location and context APIs available to your project you must add a\nreference to the Google maven repository and declare a dependency to the\nrequired APIs. For more information, see [Set Up Google Play\nservices](https://developers.google.com/android/guides/setup).\n\n### Use the location settings API\n\nBy using the location settings API, apps provide the desired QoS level and the\nAPI requests the user for the appropriate changes to the system settings. Take\nthe following steps to use the location settings API in your app:\n\n1. [Request location permissions](/training/location/permissions) in the app manifest.\n2. Set up a [`LocationRequest`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html) object, which specifies the desired QoS level.\n3. Use the location settings API to check the current settings.\n\nFor more information, see [Changing Location\nSettings](/training/location/change-location-settings#location-request) or\nsee the [Google Play Location samples](https://github.com/android/platform-samples/tree/main/samples/location/src/main/java/com/example/platform/location)\nfor example code.\n\n### Use Google Play services for complex features\n\nGoogle Play services libraries help you implement novel user experiences around\ncontext and awareness of the user's surroundings. The various [location and\ncontext](https://developers.google.com/location-context/) libraries leverage\nadditional sensors beyond just location, and do so in a power-efficient way,\nproviding your app with more accurate data while reducing the impact on battery\nlife.\n\nIncorporate these libraries in your app instead of writing custom solutions of\nyour own:\n\n- [Geofencing API](https://developers.google.com/location-context/geofencing)\n- [Activity Recognition API](https://developers.google.com/location-context/activity-recognition)\n- [Google Awareness API](https://developers.google.com/awareness)\n\n### Replace the framework location API with the fused location provider API\n\nYou can use the fused location provider API to get location data, such as\nlatitude and longitude. The fused location provider API uses a [`Location`](/reference/android/location/Location) object---just like the location\nframework API---to represent geographic location. The API provides features\nto listen for location updates as well as to get the last known location. All\nthese features make the fused location provider API a good candidate to replace\nthe components that use the framework location API with minimal changes to the\nrest of the app.\n\n[Getting the last known location](/training/location/retrieve-current) is a\ngood starting point for many experiences because it's a fast operation that uses\nlocation data requested by any client on the device. To periodically track\nlocation, your app can subscribe to\n[receive location updates](/training/location/receive-location-updates),\nwhich provides up-to-date data and enables more complex experiences.\n\n### Remove references to the framework location API\n\nReplace references to classes in the `com.google.android.location` package with\nclasses from the `com.google.android.gms.location` package, except references to\nthe [`Location`](/reference/android/location/Location) class, which the\nfused location provider API uses. You can usually remove the components that\nmanage the different providers, such as GPS and Wi-Fi, from your app. The\nlocation and context APIs automatically manage these providers.\n\nTest your app\n-------------\n\nTo run an app that uses the latest version of Google Play services, you need a\ndevice that has the Play Store app installed and a Google Account must be signed\nin. For development purposes you can use the following options:\n\n- A physical device connected to your development environment using a USB cable.\n- An emulator with the Play Store app installed.\n\nFor more information about connecting a physical device to your development\nenvironment, see [Run Apps on a Hardware Device](/studio/run/device). To\ncreate an emulator that includes the Play Store app, see [Create and Manage\nVirtual Devices](/studio/run/managing-avds)."]]