【发布时间】:2016-02-03 16:47:08
【问题描述】:
我正在构建一个需要保持位置跟踪的应用程序,即使该应用程序处于后台也是如此。基本上,我使用播放服务的 GoogleApiClient 和 LocationServices.FusedLocationApi.requestLocationUpdates 方法来调用 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)。 我该怎么做?
【问题讨论】:
标签: android intentservice google-api-client fusedlocationproviderapi