【问题标题】:Android FusedLocationApi.requestLocationUpdates on the background: Stopping IntentService when app is terminated后台的Android FusedLocationApi.requestLocationUpdates:应用程序终止时停止IntentService
【发布时间】:2016-02-03 16:47:08
【问题描述】:

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

【问题讨论】:

    标签: android intentservice google-api-client fusedlocationproviderapi


    【解决方案1】:

    试试:

    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    

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

    【讨论】:

    • 谢谢。那将是正确的电话,但我应该从哪里打电话呢?当应用程序终止时,是否有一些生命周期回调可以在 Activity/Application/IntentService 中调用它?
    • 这取决于你想在哪里停止它。挂钩按钮、服务 onDestroy 或任何您想要的地方。
    • 我还没有完全理解,抱歉......所以用例是这样的:用户启动应用程序,在第一个活动的初始化中,我在后台开始位置更新。当用户单击 Android 的左键并销毁进程时 - 我是否有回调?像 Application.onDestroy 这样的东西?因为那时我想删除LocationUpdates - 否则即使用户完全删除了应用程序,位置更新也会继续工作......
    • 我不是 Android 开发专家,但我认为您应该停止在请求它们的同一活动中接收位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2015-05-01
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多