【问题标题】:LocationUpdatesForegroundService app process is still running after closing app关闭应用程序后,LocationUpdatesForegroundService 应用程序进程仍在运行
【发布时间】:2020-01-03 16:57:19
【问题描述】:

我正在对 Google Play 定位服务进行一些调查 Google Play Location Samples

我在 LocationUpdatesForegroundService 示例应用程序上尝试以下问题:关闭应用程序后应用程序进程仍在运行。

重现它的步骤:

  • 打开应用
  • 按“请求位置更新”按钮
  • 按下设备主页按钮以在后台发送应用程序
  • 关闭应用

我的期望是应用程序进程(及其所有线程)必须停止,但事实并非如此。

关闭应用后应用进程仍在运行

当我在调用后关闭应用程序时会发生这种情况

startForeground(NOTIFICATION_ID, getNotification());

如何避免?

它看起来与用于收集位置的前台服务有关。 应用关闭时如何停止服务?

【问题讨论】:

  • 因为即使应用程序在 Android 中关闭,服务仍在后台继续运行。更多详情developer.android.com/guide/components/services
  • 我该如何阻止它?应用关闭时怎么办?
  • 在服务中使用 android:stopWithTask="true"。
  • 现在可以正常使用了,谢谢

标签: java android foreground-service android-fusedlocation


【解决方案1】:

根据documentationService 是 Android 组件,即使应用程序关闭,它也会在后台继续运行。

如果您想在应用程序从最近的任务中删除时停止服务(即应用程序终止),您必须在AndroidMenifest.xml 中使用android:stopWithTask="true" 来显式关闭服务。

例子,

<service
        android:name=".LocationUpdatesForegroundService"
        android:stopWithTask="true" />

【讨论】:

    猜你喜欢
    • 2021-04-20
    • 2013-02-03
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 2021-05-17
    • 1970-01-01
    相关资源
    最近更新 更多