问题描述
我有在主要活动中的的onCreate
方法启动后台服务的Android应用程序。
I have an android application that in onCreate
method of Main ACtivity starts a Background Service.
这是一个轻量级的服务,我想保持它运行所有的时间。所以,我回到 START_STCKY
在服务 onStartCommand()
方法。
This is a lightweight Service and I want to keep it running all the time. So , I return START_STCKY
in onStartCommand()
method of Service.
在应用程序启动和服务开始运行时,在情况下,如果只在运行服务是从设置打死,然后如果应用程序被重新启动,的的onCreate
方法程序是不调用服务未启动。
When App is launched and the service starts running,in case,if Only Running Service is killed from the Settings ,and then if App is relaunched ,the onCreate
method of app is not called and Service is not restarted.
如果我开始在onResume()活动的方法服务,它似乎并没有成为一个很好的做法继续,如果我的服务正在运行或不检查。
If I start my Service in onResume() method of Activity , it does not seem to be a good practice to keep on checking if my Service is running or not.
如何处理这种情况?我想,当服务被杀害和应用程序是重新启动,即使该应用程序在后台运行时,应该重新启动服务。
How to handle this situation? I want that when the service is killed and app is relaunched , even though the app was running in background, the service should be restarted.
推荐答案
,我使用BindService,我绑定在的onCreate
应用程序的服务和解除它 onTerminate
应用程序。
Instead of using startService ,I am using BindService , I bind the Service in onCreate
of the application and unbind it in onTerminate
of application.
这仍然运行我的服务,直到应用程序正在运行,并且用户没有给出在应用程序管理器的任何选项杀死正在运行的服务在我的应用程序的所有时光。
This keeps running my service all the time till the app is running and User is not given any option in App Manager to kill Running Services in my App.
这篇关于如何重新启动的Android服务时,它是从设置被杀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!