0

我正在构建一个应用程序,即使应用程序在后台,它也需要保持位置跟踪。基本上我使用播放服务的GoogleApiClientLocationServices.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)我怎样才能做到这一点?

4

1 回答 1

0

尝试:

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

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

于 2016-04-12T21:16:01.763 回答