删除任务会在绑定时终止后台进程
Removing task kills background process when bound
背景
我有一个使用两个进程的应用程序:
- UI
- 背景
后台进程承载两个服务:
- 一个长-运行 前台服务(从
startService
开始并使用 startForeground
成为前台),ImportantService
。它使用 START_STICKY
来确保它不会在 时被杀死
- 一个临时的进程间通信,
SettingsService
。
UI绑定到后台进程中的临时服务,使用bindService(..., ..., Context.BIND_AUTO_CREATE)
进行进程间通信。
问题
如果我在 phone 上打开最近 activity 并滑动到 kill/remove 我的 activity 绑定到 SettingsService
后,整个背景进程被杀死,ImportantService
也因此被杀死。
症状
ImportantService
被杀后重启,可能是因为START_STICKY
.
- 如果将服务移入 UI 进程,则不会发生这种情况。
- 如果我通过
startService()
启动 SettingsService
但不绑定到它,则不会发生这种情况。问题似乎是由绑定到服务触发的。
我试过的
- 使用
startService()
启动 SettingsService
,然后使用 bindService()
绑定到它。
- 使用
bindService()
的每个可用标志。
- 在
onPause()
中解除与 SettingsService
的绑定。这样可以防止问题第一次发生,但在后续测试中仍然会发生。
- 使
SettingsService
绑定到自身。
环境
- 设备:索尼 Xperia M
- OS: Android 5.1.1
- 自定义:Cyanogenmod 12.1
- 同样在 Genymotion 模拟器中重现了问题 运行 Android 4.4.4
Logcat 主缓冲区日志
06-17 07:27:22.633 678-897/? I/ActivityManager﹕ Killing 7666:com.example.myapp/u0a151 (adj 9): remove task
06-17 07:27:22.650 678-1327/? I/WindowState﹕ WIN DEATH: Window{3e26d1c9 u0 com.example.myapp/.SettingsActivity}
06-17 07:27:22.769 678-1211/? I/ActivityManager﹕ Killing 32110:com.example.myapp:backgroundprocess/u0a151 (adj 0): remove task
06-17 07:27:22.898 678-1149/? W/ActivityManager﹕ Scheduling restart of crashed service com.example.myapp/.ImportantService in 1000ms
06-17 07:27:23.979 678-696/? I/ActivityManager﹕ Start proc 7809:com.example.myapp:backgroundprocess/u0a151 for service com.example.myapp/.ImportantService
Logcat 事件缓冲区日志
06-17 20:33:08.482 I/am_finish_activity( 678): [0,395523242,1024,com.example.myapp/.SettingsActivity,clear]
06-17 20:33:08.484 I/am_destroy_activity( 678): [0,395523242,1024,com.example.myapp/.SettingsActivity,finish-imm]
06-17 20:33:08.488 I/am_kill ( 678): [0,5636,com.example.myapp,9,remove task]
06-17 20:33:08.640 I/dvm_lock_sample( 678): [system_server,1,ActivityManager,142,ActivityManagerService.java,3410,-,8682,28]
06-17 20:33:08.644 I/am_proc_died( 678): [0,5636,com.example.myapp]
06-17 20:33:08.645 I/am_kill ( 678): [0,3960,com.example.myapp:backgroundprocess,0,remove task]
06-17 20:33:08.811 I/wm_task_removed( 678): [1024,removeAppToken: last token]
06-17 20:33:08.812 I/wm_task_removed( 678): [1024,removeTask]
06-17 20:33:08.816 I/dvm_lock_sample( 678): [system_server,1,Binder_F,136,ActivityManagerService.java,1230,-,1230,27]
06-17 20:33:08.819 I/am_proc_died( 678): [0,3960,com.example.myapp:backgroundprocess]
将两个服务合并为一个似乎解决了这个问题,尽管有点混乱。
这似乎是一个 Android 错误。我已经在以下位置提交了一个问题:
Issue 178057: Process killed when task removed while bound to service with BIND_AUTO_CREATE flag.
以下是从上面采取的一些解决方法:
- In the foreground service's
onTaskRemoved()
, launch an activity on task removal
- Fixes the problem about 75% of the time.
- This closes the "Recents" activity on the user, so it alters the normal task-closing experience for the user.
- Seems to depend on timing, so it mightn't be a perfect workaround
- In the foreground service's
onTaskRemoved()
, send multiple broadcasts to a registered receiver in the application with the Intent.FLAG_RECEIVER_FOREGROUND
flag.
- Seems to nearly always work, depending on timing and the number of broadcasts you send
- Unbind from the background service before the task is removed
背景
我有一个使用两个进程的应用程序:
- UI
- 背景
后台进程承载两个服务:
- 一个长-运行 前台服务(从
startService
开始并使用startForeground
成为前台),ImportantService
。它使用START_STICKY
来确保它不会在 时被杀死
- 一个临时的进程间通信,
SettingsService
。
UI绑定到后台进程中的临时服务,使用bindService(..., ..., Context.BIND_AUTO_CREATE)
进行进程间通信。
问题
如果我在 phone 上打开最近 activity 并滑动到 kill/remove 我的 activity 绑定到 SettingsService
后,整个背景进程被杀死,ImportantService
也因此被杀死。
症状
ImportantService
被杀后重启,可能是因为START_STICKY
.- 如果将服务移入 UI 进程,则不会发生这种情况。
- 如果我通过
startService()
启动SettingsService
但不绑定到它,则不会发生这种情况。问题似乎是由绑定到服务触发的。
我试过的
- 使用
startService()
启动SettingsService
,然后使用bindService()
绑定到它。 - 使用
bindService()
的每个可用标志。 - 在
onPause()
中解除与SettingsService
的绑定。这样可以防止问题第一次发生,但在后续测试中仍然会发生。 - 使
SettingsService
绑定到自身。
环境
- 设备:索尼 Xperia M
- OS: Android 5.1.1
- 自定义:Cyanogenmod 12.1
- 同样在 Genymotion 模拟器中重现了问题 运行 Android 4.4.4
Logcat 主缓冲区日志
06-17 07:27:22.633 678-897/? I/ActivityManager﹕ Killing 7666:com.example.myapp/u0a151 (adj 9): remove task
06-17 07:27:22.650 678-1327/? I/WindowState﹕ WIN DEATH: Window{3e26d1c9 u0 com.example.myapp/.SettingsActivity}
06-17 07:27:22.769 678-1211/? I/ActivityManager﹕ Killing 32110:com.example.myapp:backgroundprocess/u0a151 (adj 0): remove task
06-17 07:27:22.898 678-1149/? W/ActivityManager﹕ Scheduling restart of crashed service com.example.myapp/.ImportantService in 1000ms
06-17 07:27:23.979 678-696/? I/ActivityManager﹕ Start proc 7809:com.example.myapp:backgroundprocess/u0a151 for service com.example.myapp/.ImportantService
Logcat 事件缓冲区日志
06-17 20:33:08.482 I/am_finish_activity( 678): [0,395523242,1024,com.example.myapp/.SettingsActivity,clear]
06-17 20:33:08.484 I/am_destroy_activity( 678): [0,395523242,1024,com.example.myapp/.SettingsActivity,finish-imm]
06-17 20:33:08.488 I/am_kill ( 678): [0,5636,com.example.myapp,9,remove task]
06-17 20:33:08.640 I/dvm_lock_sample( 678): [system_server,1,ActivityManager,142,ActivityManagerService.java,3410,-,8682,28]
06-17 20:33:08.644 I/am_proc_died( 678): [0,5636,com.example.myapp]
06-17 20:33:08.645 I/am_kill ( 678): [0,3960,com.example.myapp:backgroundprocess,0,remove task]
06-17 20:33:08.811 I/wm_task_removed( 678): [1024,removeAppToken: last token]
06-17 20:33:08.812 I/wm_task_removed( 678): [1024,removeTask]
06-17 20:33:08.816 I/dvm_lock_sample( 678): [system_server,1,Binder_F,136,ActivityManagerService.java,1230,-,1230,27]
06-17 20:33:08.819 I/am_proc_died( 678): [0,3960,com.example.myapp:backgroundprocess]
将两个服务合并为一个似乎解决了这个问题,尽管有点混乱。
这似乎是一个 Android 错误。我已经在以下位置提交了一个问题:
Issue 178057: Process killed when task removed while bound to service with BIND_AUTO_CREATE flag.
以下是从上面采取的一些解决方法:
- In the foreground service's
onTaskRemoved()
, launch an activity on task removal
- Fixes the problem about 75% of the time.
- This closes the "Recents" activity on the user, so it alters the normal task-closing experience for the user.
- Seems to depend on timing, so it mightn't be a perfect workaround
- In the foreground service's
onTaskRemoved()
, send multiple broadcasts to a registered receiver in the application with theIntent.FLAG_RECEIVER_FOREGROUND
flag.
- Seems to nearly always work, depending on timing and the number of broadcasts you send
- Unbind from the background service before the task is removed