自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
限制机会性位置信息
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
应用在请求设备的位置信息时,它可以等待请求响应,也可以通过使用主动位置信息监听器来获取机会性位置信息更新。这一功能在 Android 9 及更低版本中提供(且不受限制),并且在应用仅指定主动位置信息更新频率且将被动更新频率留空时提供(被动位置信息监听器不指定位置信息更新频率,因此不会收到机会性位置信息更新)。
从 Android 10 开始,要获取机会性位置信息更新,开发者必须指明他们需要从 FusedLocationProviderClient
类获取被动位置信息更新。
一体化位置信息提供方
应用可以通过两种方式指定从 FusedLocationProviderClient
类获取机会性位置信息更新的频率。
影响
如果您未提供 setFastestInterval
的值,则传递给应用的位置信息更新可能会大幅减少。
实现
除了使用 FusedLocationProviderClient
类 setFastestInterval
方法的建议值,您无需执行其他任何操作即可实现此功能。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Restrict opportunistic locations\n\nWhen an app requests a device's location, it can either wait for\nthe request response or, by using active location listeners, get an\nopportunistic location update. This was available (unrestricted) in\nAndroid 9 and lower, and was provided when an app specified only\nthe active location update rate, leaving the passive rate blank.\n(Passive location listeners don't receive opportunistic location\nupdates because they don't specify a location update rate.)\n\nStarting in Android 10, to get opportunistic\nlocation updates, developers must specify that they need passive location\nupdates from the **[FusedLocationProviderClient](https://developers.google.com/android/reference/com/google/android/gms/location\n/FusedLocationProviderClient)** class.\n\nFused location provider\n-----------------------\n\nApps can specify the rate at which they get opportunistic location updates from the [FusedLocationProviderClient](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient)\nclass in two ways.\n\n- Specify a value in the [setFastestInterval](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#setFastestInterval(long)) method that's lower than the value of the [setInterval](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#setInterval(long)) parameter. (Use a value of 0 ms or greater.)\n- Let the value be set automatically by leaving [setFastestInterval](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#setFastestInterval(long)) unspecified.\n\nImpact\n------\n\nIf you don't provide a value for [setFastestInterval](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#setFastestInterval(long)),\nyour app may experience a significant\nreduction in location updates delivered to it.\n\nImplementation\n--------------\n\nYou don't need to do anything to implement this feature other\nthan to use the suggested values for the [FusedLocationProviderClient](https://developers.google.com/android/reference/com/google/android/gms/location\n/FusedLocationProviderClient) class [setFastestInterval](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#setFastestInterval(long))\nmethod."]]