代码之家  ›  专栏  ›  技术社区  ›  Amin Pinjari

requestSingleUpdate在Oreo上不起作用

  •  0
  • Amin Pinjari  · 技术社区  · 7 年前

    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);
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   GigiManco    7 年前

    LocationManager API,如requestSingleUpdate,对于Oreo来说已经过时了:您必须使用 FusedLocationProviderClient 相反。

    google developer tutorials . 特别是对于移民: Migrate to location and context APIs

    我的测试证实这个补丁是向后兼容的(同样在Android 6上测试过)

        2
  •  0
  •   CoolMind    5 年前

    requestSingleUpdate deprecated

    使用getCurrentLocation(java.lang.String文件, 安卓操作系统取消信号, java.util.concurrent文件遗嘱执行人, 电池耗尽。

    看着 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()}")
        }
    }
    

    我没有任何问题 请求SingleUpdate FusedLocationProviderClient . getCurrentLocation 在API 30之前不能工作。

    推荐文章