Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 移动时未执行onLocationChange方法_Java_Android_Location_Android Fusedlocation - Fatal编程技术网

Java 移动时未执行onLocationChange方法

Java 移动时未执行onLocationChange方法,java,android,location,android-fusedlocation,Java,Android,Location,Android Fusedlocation,我试图每次在10米内执行一段代码,当我第一次运行我的应用程序时,它会执行onLocationChange中的所有代码,但当我移动超过10米后,似乎监听器不再工作,或者位置没有更新,有没有线索说明是什么导致了这个问题 @Override public void onLocationChanged(Location location) { Toast.makeText(Principal.this, "it moved: " + location.getLatitude()

我试图每次在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服务的版本也起作用,所以我认为这没有问题

添加
mLastLocationRequest.setFastTestInterval(10000)参数解决了更新问题