Android-LocationManager requestLocationUpdates回调不适用于Lollipop

时间:2018-07-13 05:56:24

标签: android geolocation locationmanager fusedlocationproviderapi

Lollipop 设备上使用LocationManager requestLocationUpdates时出现问题。 LocationListener()回调不会被调用。另外,我尝试使用FusedLocationProviderClient api,但是回调不起作用。 (权限,清单,GPS等所有依赖项均已启用)

这是我的 LocationManager

代码
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        Log.d("test", "GOTCHA");
                    }

                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {

                    }

                    @Override
                    public void onProviderEnabled(String provider) {

                    }

                    @Override
                    public void onProviderDisabled(String provider) {

                    }
                });

这是针对 FusedLocationProviderClient

FusedLocationProviderClient fusedLocationProviderClient = new FusedLocationProviderClient(getContext());
    LocationCallback mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            super.onLocationResult(locationResult);
            Log.d("test", "GOTCHA");
            if (locationResult != null) {
                Location currentLocation = locationResult.getLastLocation();
                handleCurrentLocation(currentLocation);
                fusedLocationProviderClient.removeLocationUpdates(this);
            } else {
                failToGetLocationHandler("Fail to initiate request location ...");
            }
        }

    };

    fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);

我试图在所有地方寻找解决方案,但是对我来说没有运气。但问题是,它可以在其他Android版本(如 Nougat / Oreo )上运行。据我所知,这两个API的Lollipop都没有问题(请CMIIW)。

谢谢

0 个答案:

没有答案