0
即时应用程序位于后台时,我正在构建需要保留位置跟踪的应用程序。基本上我使用播放服务' GoogleApiClient和 LocationServices.FusedLocationApi.requestLocationUpdates方法调用 IntentService。
下面的代码是怎么触发我在活动后台位置更新:Android FusedLocationApi.requestLocation在后台更新:停止应用程序终止时的IntentService
private void startLocationUpdatesOnBackground() {
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(1000)
.setFastestInterval(1000);
pendingIntent = PendingIntent.getService(this, 0,
new Intent(this, LocationBackgroundService.class), PendingIntent.FLAG_UPDATE_CURRENT);
LocationServices.FusedLocationApi.
requestLocationUpdates(mGoogleApiClient, mLocationRequest, pendingIntent);
}
它的伟大工程。
但是,当应用程序被用户终止时(通过任务管理器),我想停止这个 IntentService(称为LocationBackgroundService)。 我该怎么做?
谢谢。这将是正确的决定,但我应该从哪里调用它?当应用程序终止时是否有一些生命周期回调在Activity/Application/IntentService中调用它? – BeFree
这取决于你想阻止它的位置。钩一个按钮,服务onDestroy,或任何你想要的。 –
我还没有完全明白它,对不起...因此,用例就是这样的:用户启动应用程序,在第一个活动的init中,我在后台启动位置更新。当用户点击Android的左侧按钮并销毁进程时 - 我是否有回调?像Application.onDestroy?因为那是当我想要删除LocationUpdates - 否则位置更新继续工作,即使用户完全删除了应用程序... – BeFree