我正在测试具有不同优先级的FusedLocationProviderClient位置更新的准确性。在应用程序中,我首先使用PRIORITY_HIGH_ACCURACY
获得15次更新,然后取消请求,将优先级更改为PRIORTIY_LOW_POWER
并再次请求。我也尝试更改为ACCURACY_BALANCED_POWER_ACCURACY
,在这两种情况下,PRIORITY_HIGH_ACCURACY
在准确性方面表现最差 - 每次运行我的应用时都会这样做。
我注意到的另一件事是,在前15次更新中,准确性经常发生变化,而最后15次更新几乎同样具有相同的准确性。知道为什么会这样吗?这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtBatteryLevel = (TextView) findViewById(R.id.batteryLevel);
txtAccuracy = (TextView) findViewById(R.id.accuracy);
mFusedLocationClient =
LocationServices.getFusedLocationProviderClient(this);
mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(0);
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult){
onLocationChanged(locationResult.getLastLocation());
}
};
requestLocationUpdates();
}
public void requestLocationUpdates() {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
}
public void onLocationChanged(Location location){
int batteryLevel = Integer.valueOf(txtBatteryLevel.getText().toString());
if(batteryLevel>0) {
if (mLocationRequest.getPriority() == LocationRequest.PRIORITY_HIGH_ACCURACY) {
if (batteryLevel <= 15) {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
mLocationRequest.setInterval(0);
requestLocationUpdates();
return;
}
}
txtAccuracy.setText(String.valueOf(location.getAccuracy()));
batteryLevel--;
txtBatteryLevel.setText(String.valueOf(batteryLevel));
}
}
答案 0 :(得分:0)
检查以下代码是否对您有帮助...
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
for (Location location : locationResult.getLocations()) {
if (location != null) {
mLastKnownLocation = location;
}
}
}
};