App isn't killed when removed from recent tasks when using service
Asked Answered
L

2

6

First of all, this question is NOT about how I can prevent the app from being killed. Actually I want it to be killed.

Now the problem. My application connects to a BLE devices and I want the connection to be maintained when the app is in the background, but I don't want it to be maintained when the user removes the app from recent apps.

I implemented a Service that I run when the connection is started, which I send to foreground when app goes to background. I tried both with only bound service which is destroyed when the activity is destoyed, and also with started service which I stop in onTaskRemoved. Both cases have the same issue.

The problem is that when I wasn't using the service and removed the app from recents, it got properly killed and the BLE connection was being ended, but when I started using service, the application process seems to be still active and the BLE connection is persisted.

I did some investigation using Android Studio profiler, and I can see that after I removed the app from recents, there is no Activity and there is no Service running in my app, but the Application object is still there.

Leakanary dosn't report any memory leaks either.

It seems that it may be similar to this issue, but there was no answer. It is also a bit similar to this issue, but I use clean Android, not cordova.

I investigated the Logcat and in the flow without service I get:

HwRecentsTaskUtils: remove task send broadcast packageName={my_package_name}, userId=0,taskId=16893
ActivityManager: Killing 30468:{my_package_name}/u0a918 (adj 900): remove task
…
WindowManager: WIN DEATH: Window{bcb8d8c u0 {my_package_name}/{my_package_name}.MainActivity}

In the case with service I get:

HwRecentsTaskUtils: remove task send broadcast packageName={my_package_name}, userId=0,taskId=16894
…
ActivityThread: Remove activity client record, r= ActivityRecord{787d7fd token=android.os.BinderProxy@216a996 {{my_package_name}/{my_package_name}.MainActivity}} token= android.os.BinderProxy@216a996
MyService: service onTaskRemoved
MyService: service onDestroy

In this second case the app task seems not to be removed, and I have no idea why. What may be preventing my app from being killed when neither activity nor service is active?

Lumpish answered 9/4, 2021 at 18:7 Comment(5)
What does your Service return from onStartCommand()?Ketose
@DavidWasser I did not explicitely overrode this method, so I assume it returned START_STICKY`.Lumpish
If you are returning START_STICKY then Android will restart your Service after it is killed. Maybe you need to address this.Ketose
I don't think that this is the case. I was stopping the service by calling stopSelf in the onTaskRemoved method. Also I was logging all the lifecycle methods of that service, and 'onDestroy` was the last called, so I guess it was not restarted.Lumpish
Have the same situation with a service + BLE connection. What have you done as a result?Poona
K
4

You are talking about the OS Process hosting your application, and not the "task" (which is an Android construct). Android is responsible to killing OS processes and reclaiming them. It does it when it wants to, if it wants to, and you have no control over this. You cannot assume that your OS Process will be killed in any specific case.

You need to write your code in such a way that you can deal with anything that happens.

Ketose answered 9/4, 2021 at 18:24 Comment(3)
Ok, I hear you. So basically, what you are saying is that the first flow worked only beacuse the system decided to kill my process quickly - and in the same situation it may decide to keep the process alive longer and I have no controll over it? And problaby by using service (even after I ended it) the system decided to keep me process for a longer time?Lumpish
Hym, this kinda sucks, because Android doesn't really give us any callback for determinig when user kills the application - the only thing I could think of is keeping a started service that would wait for onTaskRemoved callback and disconnect from BLE device programatically then.Lumpish
Ok, nowadays this probably could also be handled with my main activity's viewModel onCleared. This seems to also be called my problematic situation.Lumpish
A
2

You can try this, in AndroidManifest.xml , add android:stopWithTask="true" to your service define

Altruism answered 28/7, 2021 at 1:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.