我正在使用Google的FusedLocationProvider API来接收用户设备上的位置更新。当设备GPS打开/关闭时,我在PRIORITY_HIGH_ACCURACY
和PRIORITY_BALANCED_POWER_ACCURACY
之间切换。
//location manager for GPS events
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//request to FusedLocationProvider API
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
sendLocationRequest(createLocationRequest(LocationRequest.PRIORITY_HIGH_ACCURACY, interval2, fastestInterval2));
else if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
sendLocationRequest(createLocationRequest(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY, interval, fastestInterval));
问题是当我注册接收3G网络上的位置更新和Wifi&关闭GPS,然后将优先级设置为PRIORITY_BALANCED_POWER_ACCURACY
,FusedLocationProvider
请求RESOLUTION_REQUIRED
,并启用Wifi。我想在没有wifi或GPS的3G网络上接收位置更新,即API文档中规定的100米块级精度。
我正在使用这些间隔:
private long /*BALANCED_POWER_ACCURACY*/interval=3600000, fastestInterval=300000, /*HIGH_ACCURACY*/interval2=10000, fastestInterval2=5000;
这些是相应的方法:
//creating location newRequest
private LocationRequest createLocationRequest(int priority, long interval, long fastestInterval)
{
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(interval);
locationRequest.setFastestInterval(fastestInterval);
locationRequest.setPriority(priority);
return locationRequest;
}
//sending location newRequest with needed quality of service
private void sendLocationRequest(final LocationRequest locationRequest)
{
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS: {
//requesting location updatesPendingResult<Status> statusPendingResult =
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, CurrLocationService.this);
break;
}
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
{
Toast.makeText(CurrLocationService.this,
getString(R.string.app_name)+" Error: Try using GPS/Wifi" + status.getStatusMessage() + " " + status.getStatus(), Toast.LENGTH_LONG).show();
stopSelf();
break;
}
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
{
Toast.makeText(CurrLocationService.this,
getString(R.string.app_name)+" Error: Unable to get your location updates", Toast.LENGTH_SHORT).show();
stopSelf();
break;
}
}
}
});
}