LocationCallback not getting called

给你一囗甜甜゛ 提交于 2019-12-10 01:31:44

问题


I am making a location request using FusedLocationProviderClient. I have kept a timer for 30 seconds to check if location is received within that timeframe. Code is below:

 public void requestLocationUpdates() {
    initializeLocationRequest();
    mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
    startTimer();
}

 private void initializeLocationRequest() {
    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(mContext);
    mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(5000);
    mLocationRequest.setFastestInterval(5000);

    mLocationCallback = new LocationCallback() {

        @Override
        public void onLocationResult(LocationResult locationResult) {
            synchronized (SilentCurrentLocationProvider.this) {
                super.onLocationResult(locationResult);
                Location location = locationResult.getLastLocation();
        }
    };
}

  private void startTimer() {
    if (mTimeout < 1) {
        mTimeout = mDefaultTimeout;
    }
    mTimer = new Timer();   
    mTimer.schedule(new LocationTimer(), mTimeout * 1000);
}

On few devices, location is not coming. On debugging further, I found that LocationRequest and LocationCallback are being created, but it's not coming inside onLocationResult() method.

Since this is part of a live product, it's hard to debug on every device. Even I tried restarting the phone but it doesn't work. The user tried on other applications like Ola and location was coming there.

Location Permissions have been given as well.

Can anyone tell me possible reasons for this issue?


回答1:


I was facing this same issue. In my case the device-location method/accuracy was set to Phone only rather than High accuracy or Battery saving. Change the settings to High accuracy and receive callbacks on locationCallback.




回答2:


try to place LocationCallback to "onCreate()" method.



来源:https://stackoverflow.com/questions/50870587/locationcallback-not-getting-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!