<1>Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.
<2>The most important callback methods you should override are:
<3>A service is simply a component that can run in the background even when the user is not interacting with your application.
If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service. For example, if you want to play some music, but only while your activity is running, you might create a thread in onCreate(), start running it in onStart(), then stop it in onStop(). Also consider usingAsyncTask or HandlerThread, instead of the traditional Thread class.
Remember that if you do use a service, it still runs in your application's main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.
<4>If a component calls bindService() to create the service (and onStartCommand() is not called), then the service runs only as long as the component is bound to it. Once the service is unbound from all clients, the system destroys it.
<5>The Android system will force-stop a service only when memory is low and it must recover system resources for the activity that has user focus. If the service is bound to an activity that has user focus, then it's less likely to be killed, and if the service is declared to run in the foreground (discussed later), then it will almost never be killed.
<6>By default, Android creates a process for an application when the first of its components needs to run. All components then run in that process. The name of the default process matches the package name set by the <manifest> element.
By setting this attribute to a process name that's shared with another application, you can arrange for components of both applications to run in the same process — but only if the two applications also share a user ID and be signed with the same certificate.
If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed. If the process name begins with a lowercase character, a global process of that name is created. A global process can be shared with other applications, reducing resource usage.
<7>Additionally, you can ensure that your service is private to your application only if you include the android:exported attribute and set it to "false". This is effective even if your service supplies intent filters.
<8>
Service
IntentService
This is a subclass of Service that uses a worker thread to handle all start requests, one at a time. This is the best option if you don't require that your service handle multiple requests simultaneously. All you need to do is implement onHandleIntent(), which receives the intent for each start request so you can do the background work.
<9>
The IntentService does the following:
- Creates a default worker thread that executes all intents delivered to
onStartCommand()separate from your application's main thread. - Creates a work queue that passes one intent at a time to your
onHandleIntent()implementation, so you never have to worry about multi-threading. - Stops the service after all start requests have been handled, so you never have to call
stopSelf(). - Provides default implementation of
onBind()that returns null. - Provides a default implementation of
onStartCommand()that sends the intent to the work queue and then to youronHandleIntent()implementation.
<10>however, you require your service to perform multi-threading (instead of processing start requests through a work queue), then you can extend the Service class to handle each intent.
服务管理与优化:避免ANR错误的策略与实践
本文深入探讨了Android服务的概念、关键回调方法的使用、服务的生命周期管理,以及如何通过创建独立线程来优化服务性能,减少ANR错误的发生。同时,介绍了服务的启动、绑定、生命周期回调以及服务与应用组件之间的交互机制。最后,提供了关于服务创建、生命周期管理和内存管理的重要提示,帮助开发者构建高效稳定的服务。
1800

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



