使用mFusedLocationClient.requestLocationUpdates请求位置更新返回相同的坐标。我怎么能解决它。它适用于OS 6.0以下的其他设备,但在Android OS 6.0的特定设备上返回相同的坐标
更新**我还注意到,当我在设备上安装Google Map时,它开始获取正确的坐标。这是巧合吗?
@SuppressLint("StaticFieldLeak")
private class ActivateLocation extends AsyncTask<Context, Void, Void> {
@Override
protected Void doInBackground(Context... contexts) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
SettingsClient client = LocationServices.getSettingsClient(MainActivity.this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(locationSettingsResponse -> {
if (!memoryLock) {
memoryLock = true;
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.
checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_LOCATION_PERMISSION);
} else {
gPSTracker.mFusedLocationClient.getLastLocation()
.addOnSuccessListener(location -> {
if (location != null) {
gPSTracker.location = location;
if (gPSTracker.getLatitude() != 0) {
isLocationReady = true;
runOnUiThread(MainActivity.this::openLocation);
if(notUpdatingLocation) {
notUpdatingLocation = false;
startLocationUpdates();
}
}
}
})
.addOnFailureListener(e -> runOnUiThread(() -> new Handler().postDelayed(() ->
new ActivateLocation().execute(contexts[0]), 30000)));
}
}
});
task.addOnFailureListener(e -> {
if (e instanceof ResolvableApiException) {
try {
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(MainActivity.this,
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
runOnUiThread(() -> new Handler().postDelayed(() -> new ActivateLocation().execute(contexts[0]), 30000));
}
}
});
return null;
}
}
private void startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.
checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_LOCATION_PERMISSION);
} else {
gPSTracker.mFusedLocationClient.requestLocationUpdates(mLocationRequest,
new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
for (Location location : locationResult.getLocations()) {
if (location != null) {
gPSTracker.location = location;
if (gPSTracker.getLatitude() != 0) {
isLocationReady = true;
runOnUiThread(MainActivity.this::openLocation);
}
}
}
}
},
null );
}
}