android--service 你应该记住的一些事-1
2013-11-18 22:34
537 查看
<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:
onStartCommand()
onBind()
onCreate()
onDestroy()
<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
start running it in
then stop it in
Also consider using
instead of the traditional
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
create the service (and
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
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
and set it to
This is effective even if your service supplies intent filters.
<8>
This is the base class for all services. When you extend this class, it's important that you create a new thread in which to do all the service's work, because the service uses your application's main thread, by default, which could slow the performance of
any activity your application is running.
This is a subclass of
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
which receives the intent for each start request so you can do the background work.
<9>
The
the following:
Creates a default worker thread that executes all intents delivered to
from your application's main thread.
Creates a work queue that passes one intent at a time to your
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
Provides default implementation of
returns null.
Provides a default implementation of
sends the intent to the work queue and then to your
<10>however, you require your service to perform multi-threading (instead of processing start requests through a work queue), then you can extend the
to handle each intent.
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:
onStartCommand()
onBind()
onCreate()
onDestroy()
<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 using
AsyncTaskor
HandlerThread,
instead of the traditional
Threadclass.
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:exportedattribute
and set it to
"false".
This is effective even if your service supplies intent filters.
<8>
Service
This is the base class for all services. When you extend this class, it's important that you create a new thread in which to do all the service's work, because the service uses your application's main thread, by default, which could slow the performance of
any activity your application is running.
IntentService
This is a subclass of
Servicethat
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
IntentServicedoes
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 your
onHandleIntent()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
Serviceclass
to handle each intent.
相关文章推荐
- android--service 你应该记住的一些事-2
- android--Process and Thread 你应该记住的一些事
- android--BitMap 你应该记住的一些事-1
- android--View 你应该记住的一些事
- android--Touch Events 你应该记住的一些事
- android--Content Provider 你应该记住的一些事-1
- 【android】在Service的onStartCommand()中调用stopself()应该注意的问题
- 这两天做的一些研究--Android4.x怎么做到不被杀死的Service
- Android IntentService一些资料
- Android底层启动过程(应该说是应用进程init启动后的一些步骤)
- 我们应该尽量记住一些术语的全称
- Android 关于getSystemService()方法的一些常用服务
- 一些应该记住的东西(持续更新?)
- 文章标题 android服务Service 的一些基本简介和用法
- ★Android基础篇-你应该知道的 Service
- 你应该知道的一些Android ADB 命令
- Android ListView一些应该知道的事
- Android 进阶4:Service 的一些细节
- Android中关于Service的一些事
- android之service,你应该知道的一切