Java 移动时未执行onLocationChange方法
我试图每次在10米内执行一段代码,当我第一次运行我的应用程序时,它会执行onLocationChange中的所有代码,但当我移动超过10米后,似乎监听器不再工作,或者位置没有更新,有没有线索说明是什么导致了这个问题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()
@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)参数解决了更新问题