我可以从应用#的onCreate startService()? [英] Can I startService from Application#onCreate()?
问题描述
我要开始一个服务时,我的应用程序从任何部件进行初始化。
I want to start a service when my Application is initialized from whatever component.
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
startService(new Intent(getApplicationContext(), MyService.class));
}
}
是服务于的onCreate()
状态可用?请问 super.onCreate()
初始化在AndroidManifest.xml中注册的应用程序的所有组件?
Is the Service available in the onCreate()
state? Will the super.onCreate()
initialize all components of an Application registered in the AndroidManifest.xml ?
我可以运行此code。在我的Galaxy S,但我不能保证它会在所有的设备和平台上运行,我不能找到一个Android应用程序的初始化任何文件。
I can run this code in my galaxy s, but I can't make sure it will be run in all devices and platforms, I can't find any documentation about the initialization of an Android APP.
推荐答案
是的,你就可以开始在的onCreate服务()
你这样做的方式。不保证该服务将成功启动,但 - 只要服务存在的设备上能够运行,它会的。 super.onCreate()
并没有做到这一点,需要从应用程序中启动一个服务的任何preparation。什么是你的意思是在OnCreate()状态下使用的服务?
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"?
这篇关于我可以从应用#的onCreate startService()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!