从最近的应用程序列表中滑出时,onStart在启动服务中的onCreate之后调用onTaskRemoved [英] onTaskRemoved called after onCreate in started service on swipe-out from recent app list

查看:78
本文介绍了从最近的应用程序列表中滑出时,onStart在启动服务中的onCreate之后调用onTaskRemoved的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从Activity启动的服务,该服务运行正常onCreateonStartCommand正常.在设置->正在运行的应用中,其显示为1 process and 1 service.
现在,当我从应用程序最近列表中滑出应用程序时.该服务将重新创建,在设置->运行的应用程序中,它显示0 process and 1 service restarting...并调用了onCreateonStartCommandonTaskRemoved.

I have a service started from an Activity, the service is running fine onCreate, onStartCommand called properly. In settings-->running app its showing 1 process and 1 service.
Now when I swipe out my application from apps recent list. The service recreates, In settings-->running app it shows 0 process and 1 service restarting... And onCreate, onStartCommand, onTaskRemoved called.

现在问题出在拔出应用程序后立即调用一个应用程序onTaskRemoved然后调用onCreate,而另一个应用程序onTaskRemoved需要花费时间并在onCreate之后调用.

Now the issue is in one app onTaskRemoved called immediately after swiping app out then onCreate called, and in other app onTaskRemoved takes time and called after onCreate.

为什么会有这种不同的行为?任何建议都会对我有帮助.请提供您的评论和意见.

Why this different behavior is there ? Any suggestion will help me. Please provide your comments and observations.

08-26 15:43:48.611: D/PassiveService(15359): PASS_IVE, onCreate...
08-26 15:43:48.611: I/PassiveService(15359): PASS_IVE, registerReceivers...

刷卡后

08-26 15:44:05.789: D/PassiveService(15499): PASS_IVE, onCreate...
08-26 15:44:05.809: I/PassiveService(15499): PASS_IVE, registerReceivers...
08-26 15:44:05.839: I/PassiveService(15499): PASS_IVE, onTaskRemoved...

推荐答案

调用正在运行的服务的 onTaskRemoved (当应用程序从最近的应用程序中清除时)通常会延迟执行任何与UI相关的繁重工作或向服务中的接收方广播消息.

Calling onTaskRemoved of the running service(when app gets swiped out from recent apps) will be generally delayed if we are performing any heavy UI related stuff or broadcasting messages to receivers in service.

例如,假设您正在从Web服务器下载大小为50MB的文件,那么每次您读取1024bytes的流数据作为缓冲区并且正在将数据写入设备中的文件时,都从Web服务器下载文件

E.g , Assume you are downloading the file of size 50MB from web server, so from web server everytime you are reading 1024bytes of stream data as buffer and that data you are writing to a file in device.

与此同时,您正在更新UI线程的进度,这意味着您要更新到UI线程的每个KB都将导致应用程序冻结.

Meanwhile you are updating the progress to the UI thread which means every KB you are updating to the UI thread, this will cause the application to freeze.

因此,如果您从最近的应用程序列表中滑出,则系统将尝试停止该服务,但是由于该服务与UI线程处于联系状态,因此系统将无法停止该服务,但是,即使旧服务尚未停止,它仍会创建新服务.

So in between if you swipe-out from recent app list , then the system will try to stop the service but since the service is in-contact with the UI thread, the system will be unable to stop that service, but it will create new service eventhough the old service is not yet stopped.

一旦旧服务完成了与UI线程的通信,然后将调用onTaskRemoved()并将停止旧服务.新服务将在后台运行.

Once old service finishes the communication with the UI thread then onTaskRemoved() gets called and the old service will be stopped. The new service will be running in the background.

这篇关于从最近的应用程序列表中滑出时,onStart在启动服务中的onCreate之后调用onTaskRemoved的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆