简体   繁体   中英

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

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM