【问题标题】:onLocationChange method is not executing when I move当我移动时,onLocationChange 方法没有执行
【发布时间】:2019-02-02 05:31:23
【问题描述】:

我试图每次在 10 米内执行一段代码,当我第一次运行我的应用程序时,它会执行 onLocationChange 内的所有代码,但在我移动超过 10 米后,监听器似乎无法正常工作不再更新或位置未更新,是否有任何线索可能导致此问题?

@Override
    public void onLocationChanged(Location location) {
        Toast.makeText(Principal.this, "it moved: " + location.getLatitude() + "" + location.getLongitude(), Toast.LENGTH_SHORT).show();
        Log.i(TAG, "onLocationChanged: "+location.getLatitude()+""+location.getLongitude());
        getPlaceByTimer();

    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

        Toast.makeText(this, "connected", Toast.LENGTH_SHORT).show();
        startLocationUpdate();


    }

    private void initLocationRequest() {
        mLastLocationRequest = new LocationRequest();
        mLastLocationRequest.setSmallestDisplacement(10);
      mLastLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    }


    private void startLocationUpdate() {

        initLocationRequest();

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLastLocationRequest, this);
    }

FusedLocationApi 似乎已被弃用,但我读到的是它也适用于 Google Play 服务的版本,所以我认为那里没有问题

【问题讨论】:

    标签: java android location android-fusedlocation


    【解决方案1】:

    似乎在 mLastLocationRequest 中添加mLastLocationRequest.setFastestInterval(10000); 参数解决了更新问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      相关资源
      最近更新 更多