Application
Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's <application> tag, which will cause that class to be instantiated for you when the process for your application/package is created.
There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a android.content.Context which internally usesContext.getApplicationContext() when first constructing the singleton.
------------------------------
上面是Application的doc解释。在application中,oncreate函数中有说明,在这个函数中不要做太多的工作,因为这个oncreate函数的性能直接决定了我们的app启动第一个activity、service或者receiver的速度。doc如下:
Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created. Implementations should be as quick as possible (for example using lazy initialization of state) since the time spent in this function directly impacts the performance of starting the first activity, service, or receiver in a process. If you override this method, be sure to call super.onCreate().
------------------------------
Service
Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. More information on this can be found in Processes and Threads. The IntentService class is available as a standard implementation of Service that has its own thread where it schedules its work to be done.
根据上面的doc来看,service也是运行在主线程中的,所以任何要求长时间的操作,都要放到其他线程中来做。
本文介绍了Android中Application类的作用及使用建议,强调了避免在onCreate方法中执行耗时操作的重要性。同时,还讨论了Service组件如何正确处理后台任务,确保不会阻塞主线程。
2万+

被折叠的 条评论
为什么被折叠?



