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?
Service
return fromonStartCommand()
? – KetoseService
after it is killed. Maybe you need to address this. – KetosestopSelf
in theonTaskRemoved
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