requestSingleUpdate在Oreo上不起作用

时间:2018-08-14 09:08:12

标签: android gps location android-8.0-oreo android-8.1-oreo

我开发了一个应用程序,可以向我发送一次位置信息,一切正常,但是我没有在Oreo设备中接收位置信息。提供了位置信息许可,并且GPS也已打开

Criteria criteria = new Criteria();
                criteria.setAccuracy(Criteria.ACCURACY_FINE);
                Log.d(TAG, "requestSingleUpdate: ");
                locationManager.requestSingleUpdate(criteria, new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        callback.onNewLocationAvailable(new GPSCoordinates(location.getLatitude(), location.getLongitude()));
                    }

                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {
                        callback.onSomethingWrong("Status Changed" + status);
                    }

                    @Override
                    public void onProviderEnabled(String provider) {
                        callback.onSomethingWrong("Provider Enabled " + provider);
                    }

                    @Override
                    public void onProviderDisabled(String provider) {
                        callback.onSomethingWrong("Provider Disabled" + provider);
                    }
                }, null);

3 个答案:

答案 0 :(得分:1)

LocationManager API(例如requestSingleUpdate)对于Oreo已过时:您必须使用 FusedLocationProviderClient

您可以参考google developer tutorials。特别是对于迁移: Migrate to location and context APIs

我的测试确认此修复程序向后兼容(也已在Android 6上进行了测试)

答案 1 :(得分:0)

requestSingleUpdate 在 API 30 中为 deprecated

<块引用>

使用 getCurrentLocation(java.lang.String, android.os.CancellationSignal, java.util.concurrent.Executor, java.util.function.Consumer) 而不是,因为它不会带来风险 极度耗电。

查看 https://medium.com/@priyavrat.acharya01/using-new-on-demand-location-update-api-introduced-in-android-11-69c65b3787aa 我们可以看到:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    // Fetching the one time current location.
    val locationManager: LocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager

    // Handle runtime location permission, before calling this method, otherwise it will through the SecurityException.
    // Provider, executor and Consumer<Location> cannot be passed null.
    // CancelationRequest object can be passed as null.          
    locationManager.getCurrentLocation(LocationManager.NETWORK_PROVIDER, null, this.mainExecutor, locationCallback)
} else {
    // Keep using the legacy code, such as LocationManager.requestSingleUpdate()
}

private val locationCallback = Consumer<Location> { location ->
    location?.let {
        Timber.d("Latitude: ${it.latitude.toString()}, longitude: ${it.longitude.toString()}")
    }
}

我在 Oreo 上使用 requestSingleUpdate 没有问题。我没有测试代码,因为也迁移到FusedLocationProviderClientgetCurrentLocation 在 API 30 之前无法使用。

答案 2 :(得分:-1)

您确定您具有权限吗?位置被认为是危险的允许,因此应在运行时请求。在此处查看更多信息:

https://developer.android.com/guide/topics/permissions/overview#normal-dangerous