迁移至 Google Play 服务地理位置 API 和上下文 API
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Google 建议您在需要位置信息服务的应用中使用 Google Play 服务中的 Location API 和 Context API。如果您的应用使用框架位置 API,请务必先迁移到 Google Play 服务,以便充分利用由 Google 提供强有力技术后盾的最新功能。
使用 Google Play 服务是在应用中获取位置信息服务的首选方式,原因如下:
- Google Play 服务提供了一个简单的接口和更简洁的 API Surface。
- 您可以指定所需的服务质量,API 会为您管理底层技术。
- Google Play 服务 API 针对性能和电池用量进行了优化。
- Google Play 服务 API 得到了积极维护。Google 还在不断改进算法并增加更多功能。
更新应用
以下步骤介绍了更新应用以使用 Location API 和 Context API 的过程:
- 在您的项目中设置 Google Play 服务。
- 使用 Location Settings API 验证当前的位置信息设置。
- 将 Google Play 服务用于地理围栏、运动状态识别和感知等复杂功能。
- 将 Framework Location API 替换为 Fused Location Provider API。
- 移除对 Framework Location API 的引用。
在项目中设置 Google Play 服务
如需在项目中使用 Location API 和 Context API,您必须添加对 Google Maven 制品库的引用,并声明依赖所需 API。如需了解详情,请参阅设置 Google Play 服务。
使用 Location Settings API
通过使用 Location Settings API,应用可提供所需的服务质量 (QoS) 级别,并且此 API 会请求用户对系统设置进行适当的更改。如需在应用中使用 Location Settings API,请按以下步骤操作:
- 在应用清单中请求位置权限。
- 设置用于指定所需服务质量 (QoS) 级别的
LocationRequest
对象。
- 使用 Location Settings API 检查当前设置。
如需了解详情,请参阅更改位置信息设置;如需查看示例代码,请参阅 Google Play 位置示例。
使用 Google Play 服务实现复杂功能
Google Play 服务库可帮助您围绕情境和用户周围环境感知来实现全新的用户体验。各种位置信息和情境库除了利用位置信息外,还会利用其他传感器,而且是以省电的方式这样做,从而为您的应用提供更准确的数据,同时减少对电池续航时间的影响。
在应用中纳入这些库,而不是自行编写自定义解决方案:
将 Framework Location API 替换为 Fused Location Provider API
您可以使用 Fused Location Provider API 获取纬度和经度等位置数据。Fused Location Provider API 像 Location Framework API 一样,使用 Location
对象来表示地理位置。该 API 提供的功能可监听位置信息更新以及获取最近一次的已知位置。所有这些功能使 Fused Location Provider API 非常适合替代使用 Framework Location API 的组件,需对应用其余部分做出的改动非常少。
获取最近一次的已知位置是在许多方面实现良好体验的基础,因为它是一项快捷操作,可以使用设备上任意客户端请求的位置数据。如需定期跟踪位置信息,您的应用可以订阅接收位置信息更新,获取最新数据并实现更复杂的体验。
移除对 Framework Location API 的引用
将对 com.google.android.location
软件包中类的引用替换为对 com.google.android.gms.location
软件包中类的引用,但对 Fused Location Provider API 使用的 Location
类的引用除外。通常情况下,您可以从应用中移除用于管理不同提供程序(例如 GPS 和 Wi-Fi)的组件。Location API 和 Context API 会自动管理这些提供程序。
测试应用
如需运行使用最新版 Google Play 服务的应用,您需要安装了 Play 商店应用的设备并且必须登录 Google 账号。出于开发目的,您可以使用以下选项:
- 使用 USB 线连接到开发环境的实体设备。
- 安装了 Play 商店应用的模拟器。
如需详细了解如何将实体设备连接到开发环境,请参阅在硬件设备上运行应用。如需创建包含 Play 商店应用的模拟器,请参阅创建和管理虚拟设备。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2023-11-01。
[[["易于理解","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):2023-11-01。"],[],[],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)."]]