嗨,我正在我的应用程序中集成位置服务。为此,我正在使用 Google 的 fuse 定位服务。
我的实现如下:
public class LocationNotifyService extends Service implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
//show error dialog if GoolglePlayServices not available
if (isGooglePlayServicesAvailable()) {
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
}
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
startLocationUpdates();
}
}
我想要实现的是当我的应用程序运行时我想更改LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
为LocationRequest.HIGH_ACCURACY
,当应用程序在后台再次更改为PRIORITY_BALANCED_POWER_ACCURACY
. 更新间隔也一样。如果应用程序正在运行,它应该经常更新位置,当它在后台时,它应该在一段时间后更新。
所以简而言之,这是一项服务,我想在服务运行时更改保险丝位置设置而不重新启动服务。
我完全可以让应用程序处于前台或后台。唯一的问题是切换优先级。