您的位置:首页 > 移动开发 > Android开发

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 
onCreate()
,
start running it in 
onStart()
,
then stop it in 
onStop()
.
Also consider using
AsyncTask
 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

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 
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 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 
Service
 class
to handle each intent.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android service