Android FusedLocationApi.requestLocationUpdates在后台:终​​止应用程序时停止IntentService

时间:2016-02-03 16:47:09

标签: android intentservice google-api-client fusedlocationproviderapi

即使应用程序在后台,我也需要构建一个需要保持位置跟踪的应用。基本上我正在使用游戏服务' 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)我该怎么做?

1 个答案:

答案 0 :(得分:0)

尝试:

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

http://developer.android.com/intl/es/training/location/receive-location-updates.html#stop-updates