简体   繁体   English

使用服务时从最近的任务中删除应用程序不会被杀死

[英]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.我的应用程序连接到 BLE 设备,我希望在应用程序处于后台时保持连接,但我不希望在用户从最近的应用程序中删除应用程序时保持连接。

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 .我尝试了仅在活动被破坏时销毁的bound service ,以及我在onTaskRemoved中停止的已started service 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.问题是当我不使用该服务并从最近删除该应用程序时,它被正确杀死并且BLE连接被终止,但是当我开始使用服务时,应用程序进程似乎仍然处于活动状态并且BLE连接持续存在。

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.我使用 Android Studio 分析器进行了一些调查,我可以看到在我从最近删除应用程序后,我的应用程序中没有 Activity 并且没有运行服务,但应用程序 object 仍然存在。

Leakanary dosn't report any memory leaks either. Leakanary 也没有报告任何 memory 泄漏。

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.它也有点类似于这个问题,但我使用干净的 Android,而不是 cordova。

I investigated the Logcat and in the flow without service I get:我调查了 Logcat 并在没有服务的情况下得到:

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 构造)。 Android is responsible to killing OS processes and reclaiming them. Android 负责杀死操作系统进程并回收它们。 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您可以试试这个,在AndroidManifest.xml中,将android:stopWithTask="true"添加到您的服务定义中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从最近的应用程序中删除应用程序时,“前台”服务将被终止 - “foreground” service is killed when App is removed from recent apps 从最近的任务中删除应用程序时,Android 通知被取消 - Android notification gets cancelled when app is removed from recent tasks 从最近的任务中杀死应用程序后如何重新启动服务 - How to restart service after the app is killed from recent tasks 即使应用程序被杀死,我怎样才能让我的应用程序运行? 从最近的应用程序中删除时在某种意义上被杀死 - How can i make my app run even when app is killed? killed in the sense when it is removed from recent apps 从最近的应用程序列表中删除后,我的应用程序被杀死 - My application is getting killed when removed from recent apps list 从最近的应用程序中删除后,请不要杀死应用程序 - Don't kill app when removed from recent apps 从最近的任务中删除应用程序时,Android静态BroadcastReceiver无法正常工作 - Android Static BroadcastReceiver not working when Application is removed from Recent Tasks Android 6.0:正在删除应用程序时删除前台服务 - Android 6.0 : Foreground service is being removed when app is being killed 当最新任务列表中的应用被杀死时,Android后台服务永远不会启动 - Android background service never starts when killed app from recent task list 当用户从最近的应用程序中关闭应用程序时,Sticky Service被杀死 - Sticky Service is getting killed when user closes app from recent apps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM