【问题标题】:LocationCallback not getting calledLocationCallback 没有被调用
【发布时间】:2018-11-25 00:06:42
【问题描述】:

我正在使用 FusedLocationProviderClient 发出位置请求。我已将计时器保持 30 秒,以检查是否在该时间范围内收到位置。 代码如下:

 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);
}

在少数设备上,位置不会出现。在进一步调试时,我发现 LocationRequestLocationCallback 正在创建,但它没有进入 onLocationResult() 方法。

由于这是实时产品的一部分,因此很难在每台设备上进行调试。 即使我尝试重新启动手机,但它不起作用。用户尝试了其他应用程序,如 Ola,并且位置正在出现。

位置权限也已授予

谁能告诉我这个问题的可能原因?

【问题讨论】:

标签: android google-location-services fusedlocationproviderclient


【解决方案1】:

您只需要请求以下权限:

Manifest.permission.ACCESS_FINE_LOCATION

我遇到了同样的问题,我正在请求这两个权限:

Manifest.permission.ACCESS_COARSE_LOCATION

当我删除 COARSE LOCATION 后,我几乎立即得到了更新。

【讨论】:

  • 我们是否应该在清单中添加ACCESS_COARSE_LOCATION(可能还有INTERNET)权限?
【解决方案2】:

我也面临同样的问题。在我的例子中,设备定位方法/精度设置为仅限手机,而不是高精度省电。将设置更改为 高精度 并在 locationCallback 上接收回调。

【讨论】:

  • 我们该怎么做?
【解决方案3】:

在装有 Android Pie 的手机上遇到了同样的问题。已授予位置权限(通过转到应用程序设置手动)并且位置处于高精度状态,但未获得位置更新。 在我添加代码以检查在请求位置更新之前是否授予位置权限后得到修复。

if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_FINE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {
    // Permission is not granted. Request for permission
}

【讨论】:

    【解决方案4】:

    这发生在我关闭位置时。不是应用程序的位置权限,而是 android 设备本身的位置。要求用户激活 GPS 已为我修复:

    protected void onCreate(Bundle savedInstanceState) {
        //...
        final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
    
        if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            buildAlertMessageNoGps();
        }
    }
    
    private void buildAlertMessageNoGps() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                        startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                        dialog.cancel();
                    }
                });
        final AlertDialog alert = builder.create();
        alert.show();
    }
    

    来自here的解决方案。

    您可能还需要关闭 GPS,然后再次打开以触发对话框以提高定位精度:

    【讨论】:

    • 工作就像一个魅力!
    猜你喜欢
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 2012-12-16
    相关资源
    最近更新 更多