如何处理code当应用程序被刷android系统中被杀? [英] How to handle code when app is killed by swiping in android?
问题描述
如果我的应用程序正在运行,并且我preSS home键,应用程序进入后台。现在,如果长时间preSS home键,并从最近的应用程序列表刷卡吧,没有人像的onPause事件()的
,<$ C $杀应用程序C>的onStop()或的onDestroy()
被调用,而这个过程被终止。所以,如果我希望我的服务停止,杀死通知和注销的听众,我该怎么办呢?我看了好几篇文章和博客,但没有得到任何有用的信息,我还没有发现任何关于它的文档。
任何帮助将是AP preciated。
提前致谢。
If my app is running and i press home button, the app goes in background. Now if a long press the home button and kill the app by swiping it from the recent app list, none of the events like onPause()
, onStop()
or onDestroy()
gets called rather the process is terminated. So if i want my services to stop, kill notifications and unregister listeners, how can i do that? I read quite a few articles and blogs but didn't get any useful information and I haven't found any documentation about it.
Any help would be appreciated.
Thanks in advance.
推荐答案
我刚刚解决了类似的问题的种类。
I just resolved a similar kind of issue.
下面是你可以做什么,如果它的只是停止服务:当应用程序被从最近的应用程序列表刷卡。
Here is what you can do if its just about stopping service when application is killed by swiping from Recent app list.
在你的清单文件,守旗 stopWithTask
为真
的服务。像:
Inside your Manifest file, keep flag stopWithTask
as true
for Service. Like:
<service
android:name="com.myapp.MyService"
android:stopWithTask="true" />
但正如你说你要注销的听众和停止的通知等,我建议这种方法:
But as you say you want to unregister listeners and stop notification etc, I would suggest this approach:
-
在你的清单文件,守旗
stopWithTask
为假
的服务。像:
<service
android:name="com.myapp.MyService"
android:stopWithTask="false" />
现在在你的为MyService
服务,重写方法 onTaskRemoved
。 (这将仅当发射 stopWithTask
设置为假
)。
Now in your MyService
service, override method onTaskRemoved
. (This will be fired only if stopWithTask
is set to false
).
public void onTaskRemoved(Intent rootIntent) {
//unregister listeners
//do any other cleanup if required
//stop service
stopSelf();
}
请参照<一href="http://stackoverflow.com/questions/26842675/continue-service-even-if-application-is-cleared-from-recent-app">my问题了解更多详细信息,其中包含code另一部分了。
Refer my question for more details, which contains other part of code, too.
希望这有助于。
这篇关于如何处理code当应用程序被刷android系统中被杀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!