Can I startService from Application#onCreate()?

こ雲淡風輕ζ 提交于 2019-11-30 11:23:58

Yes, you can start a service in onCreate() the way you are doing so. There is no guarantee that the service will successfully start though - as long as the service exists on the device and is able to run, it will. super.onCreate() does not do any preparation that is required to start a service from within your application. What do you mean by "Is the service available in the onCreate() state"?

In short Yes, the Context.startService() but the doc also says that if it returns null the service is not available.

If the service is being started or is already running, the ComponentName of the actual service that was started is returned; else if the service does not exist null is returned

This API isn't meant to vary between devices so you can be confident in what you're experiencing on the emulator and devices you have. The on caveat is to remember that Services that require "lots" of resources may act differently, ie cpu time or memory, etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!