Android 在服务运行时更改保险丝位置设置
嗨,我正在我的应用程序中集成位置服务。为此,我正在使用谷歌的fuse定位服务 我的实现如下:Android 在服务运行时更改保险丝位置设置,android,service,android-location,location-services,android-fusedlocation,Android,Service,Android Location,Location Services,Android Fusedlocation,嗨,我正在我的应用程序中集成位置服务。为此,我正在使用谷歌的fuse定位服务 我的实现如下: public class LocationNotifyService extends Service implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { protected vo
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\u BALANCED\u POWER\u accurity
更改为LocationRequest.HIGH\u accurity
,当应用程序再次处于后台时,将其更改为PRIORITY\u BALANCED\u POWER\u accurity
。更新间隔也是如此。若应用程序运行,它应该经常更新位置,当它在后台时,它应该在一段时间后更新
简而言之,这是一项服务,我想在服务运行时更改保险丝位置设置,而不重新启动服务
我完全明白应用程序是在前台还是在后台。唯一的问题是切换优先级。您可以通过意向与您的服务交互来解决这一问题。并以所需的精度取消注册/注册位置请求。您可以在活动中实施
onPause()
方法,以检测用户何时离开应用程序并执行某些操作
onPause()是处理离开活动的用户的地方。最重要的是,此时应提交用户所做的任何更改
在此方法中,从此处与您的服务通信(发送应用程序不再运行的信息)
为此,您可以使用starService(intent)
若服务此时未运行,它将启动服务,但若服务正在运行,它将不会重新启动,它将只调用服务中的onStartCommand()
在服务中-您可以从您的
onStartCommand()
中的intent extra获取值。我通过创建新的GoogleAppClient和新的LocationRequest解决了我的问题
在onStartCommand()中
当我的应用程序进入后台时,我会再次启动我的服务,检查应用程序是否在后台,并根据它更改我的设置
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (isGooglePlayServicesAvailable()) {
if (!isAppIsInBackground(this)) {
//stopLocationUpdates();
mLocationRequest.setInterval(FOREGROUND_INTERVAL);
mLocationRequest.setFastestInterval(FOREGROUND_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
} else {
mLocationRequest.setInterval(BACKGROUND_INTERVAL);
mLocationRequest.setFastestInterval(BACKGROUND_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
return super.onStartCommand(intent, flags, startId);
}
您能详细说明一下吗?如果用户从应用程序托盘销毁应用程序会怎么样?无论活动是暂停还是正在销毁,都会调用onPause()。您应该在这里亲自体验阅读活动生命周期文档。执行所有与活动周期相关的方法,并添加Log.e();对他们的陈述。然后暂停或销毁你的应用程序。很明显,什么叫做“第一个”和“最后一个”。我要说的是,我们正在活动A中启动服务。如果用户在活动B中,那么它会破坏应用程序。它不会调用暂停活动A。碎片!使用片段和单个活动。