android 开发者后台,Services

A

A service can essentially take two forms:

Started

A service is "started" when an application component (such as an activity) starts it by

calling

Bound

A service is "bound" when an application component binds to it by calling

Although this documentation generally discusses these two types of services separately, your

service can work both ways—it can be started (to run indefinitely) and also allow binding.

It's simply a matter of whether you implement a couple callback methods:

Regardless of whether your application is started, bound, or both, any application component

can use the service (even from a separate application), in the same way that any component can use

an activity—by starting it with an Declaring the service in the

manifest.

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.

The Basics

Should you use a service or a thread?

A service is simply a component that can run in the background even when the user is not

interacting with your application. Thus, you should create a service only if that is what you

need.

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 Processes and

Threading document for more information about threads.

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.

To create a service, you must create a subclass of

The system calls this method when another component, such as an activity,

requests that the service be started, by calling

The system calls this method when another component wants to bind with the

service (such as to perform RPC), by calling

The system calls this method when the service is first created, to perform one-time setup

procedures (before it calls either

The system calls this method when the service is no longer used and is being destroyed.

Your service should implement this to clean up any resources such as threads, registered

listeners, receivers, etc. This is the last call the service receives.

If a component starts the service by calling

If a component calls

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.

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.

Otherwise, if the service was started and is long-running, then the system will lower its position

in the list of background tasks over time and the service will become highly susceptible to

killing—if your service is started, then you must design it to gracefully handle restarts

by the system. If the system kills your service, it restarts it as soon as resources become

available again (though this also depends on the value you return from Processes and Threading

document.

In the following sections, you'll see how you can create each type of service and how to use

it from other application components.

Declaring a service in the manifest

Like activities (and other components), you must declare all services in your application's

manifest file.

To declare your service, add a element

as a child of the

element. For example:

...

...

There are other attributes you can include in the element to

define properties such as permissions required to start the service and the process in

which the service should run. The android:name

attribute is the only required attribute—it specifies the class name of the service. Once

you publish your application, you should not change this name, because if you do, you might break

some functionality where explicit intents are used to reference your service (read the blog post, Things

That Cannot Change).

See the element

reference for more information about declaring your service in the manifest.

Just like an activity, a service can define intent filters that allow other components to

invoke the service using implicit intents. By declaring intent filters, components

from any application installed on the user's device can potentially start your service if your

service declares an intent filter that matches the intent another application passes to

If you plan on using your service only locally (other applications do not use it), then you

don't need to (and should not) supply any intent filters. Without any intent filters, you must

start the service using an intent that explicitly names the service class. More information

about starting a service is discussed below.

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.

For more information about creating intent filters for your service, see the Intents and Intent Filters

document.

Creating a Started Service

Targeting Android 1.6 or lower

If you're building an application for Android 1.6 or lower, you need

to implement

For more information about providing compatibility with versions of Android older than 2.0, see

the

A started service is one that another component starts by calling

When a service is started, it has a lifecycle that's independent of the

component that started it and the service can run in the background indefinitely, even if

the component that started it is destroyed. As such, the service should stop itself when its job

is done by calling

An application component such as an activity can start the service by calling

For instance, suppose an activity needs to save some data to an online database. The activity can

start a companion service and deliver it the data to save by passing an intent to

Caution: A services runs in the same process as the application

in which it is declared and in the main thread of that application, by default. So, if your service

performs intensive or blocking operations while the user interacts with an activity from the same

application, the service will slow down activity performance. To avoid impacting application

performance, you should start a new thread inside the service.

Traditionally, there are two classes you can extend to create a started 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.

This is a subclass of

The following sections describe how you can implement your service using either one for these

classes.

Extending the IntentService class

Because most started services don't need to handle multiple requests simultaneously

(which can actually be a dangerous multi-threading scenario), it's probably best if you

implement your service using the

The

Creates a default worker thread that executes all intents delivered to

Creates a work queue that passes one intent at a time to your

Stops the service after all start requests have been handled, so you never have to call

Provides default implementation of

Provides a default implementation of

All this adds up to the fact that all you need to do is implement

Here's an example implementation of

public class HelloIntentService extends IntentService {

/**

* A constructor is required, and must call the super

That's all you need: a constructor and an implementation of

If you decide to also override other callback methods, such as

For example,

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

return super.onStartCommand(intent,flags,startId);

}

Besides

In the next section, you'll see how the same kind of service is implemented when extending

the base

Extending the Service class

As you saw in the previous section, using

For comparison, the following example code is an implementation of the

public class HelloService extends Service {

private Looper mServiceLooper;

private ServiceHandler mServiceHandler;

// Handler that receives messages from the thread

private final class ServiceHandler extends Handler {

public ServiceHandler(Looper looper) {

super(looper);

}

@Override

public void handleMessage(Message msg) {

// Normally we would do some work here, like download a file.

// For our sample, we just sleep for 5 seconds.

long endTime = System.currentTimeMillis() + 5*1000;

while (System.currentTimeMillis() < endTime) {

synchronized (this) {

try {

wait(endTime - System.currentTimeMillis());

} catch (Exception e) {

}

}

}

// Stop the service using the startId, so that we don't stop

// the service in the middle of handling another job

stopSelf(msg.arg1);

}

}

@Override

public void onCreate() {

// Start up the thread running the service. Note that we create a

// separate thread because the service normally runs in the process's

// main thread, which we don't want to block. We also make it

// background priority so CPU-intensive work will not disrupt our UI.

HandlerThread thread = new HandlerThread("ServiceStartArguments",

Process.THREAD_PRIORITY_BACKGROUND);

thread.start();

// Get the HandlerThread's Looper and use it for our Handler

mServiceLooper = thread.getLooper();

mServiceHandler = new ServiceHandler(mServiceLooper);

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

// For each start request, send a message to start a job and deliver the

// start ID so we know which request we're stopping when we finish the job

Message msg = mServiceHandler.obtainMessage();

msg.arg1 = startId;

mServiceHandler.sendMessage(msg);

// If we get killed, after returning from here, restart

return START_STICKY;

}

@Override

public IBinder onBind(Intent intent) {

// We don't provide binding, so return null

return null;

}

@Override

public void onDestroy() {

Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();

}

}

As you can see, it's a lot more work than using

However, because you handle each call to

Notice that the

If the system kills the service after do not recreate the service, unless there are pending

intents to deliver. This is the safest option to avoid running your service when not necessary

and when your application can simply restart any unfinished jobs.

If the system kills the service after do not redeliver the last intent.

Instead, the system calls

If the system kills the service after

For more details about these return values, see the linked reference documentation for each

constant.

Starting a Service

You can start a service from an activity or other application component by passing an

For example, an activity can start the example service in the previous section (HelloSevice) using an explicit intent with

Intent intent = new Intent(this, HelloService.class);

startService(intent);

The

If the service does not also provide binding, the intent delivered with

Multiple requests to start the service result in multiple corresponding calls to the service's

Stopping a service

A started service must manage its own lifecycle. That is, the system does not stop or

destroy the service unless it must recover system memory and the service

continues to run after

Once requested to stop with

However, if your service handles multiple requests to startId

delivered to

Caution: It's important that your application stops its services

when it's done working, to avoid wasting system resources and consuming battery power. If necessary,

other components can stop the service by calling

For more information about the lifecycle of a service, see the section below about Managing the Lifecycle of a Service.

Creating a Bound Service

A bound service is one that allows application components to bind to it by calling start it by calling

You should create a bound service when you want to interact with the service from activities

and other components in your application or to expose some of your application's functionality to

other applications, through interprocess communication (IPC).

To create a bound service, you must implement the not need to stop a bound service in the way you must when the service is started

through

To create a bound service, the first thing you must do is define the interface that specifies

how a client can communicate with the service. This interface between the service

and a client must be an implementation of

Multiple clients can bind to the service at once. When a client is done interacting with the

service, it calls

There are multiple ways to implement a bound service and the implementation is more

complicated than a started service, so the bound service discussion appears in a separate

document about Bound Services.

Sending Notifications to the User

Once running, a service can notify the user of events using Toast Notifications or Status Bar Notifications.

A toast notification is a message that appears on the surface of the current window for a

moment then disappears, while a status bar notification provides an icon in the status bar with a

message, which the user can select in order to take an action (such as start an activity).

Usually, a status bar notification is the best technique when some background work has completed

(such as a file completed

downloading) and the user can now act on it. When the user selects the notification from the

expanded view, the notification can start an activity (such as to view the downloaded file).

See the Toast Notifications or Status Bar Notifications

developer guides for more information.

Running a Service in the Foreground

A foreground service is a service that's considered to be something the

user is actively aware of and thus not a candidate for the system to kill when low on memory. A

foreground service must provide a notification for the status bar, which is placed under the

"Ongoing" heading, which means that the notification cannot be dismissed unless the service is

either stopped or removed from the foreground.

For example, a music player that plays music from a service should be set to run in the

foreground, because the user is explicitly aware

of its operation. The notification in the status bar might indicate the current song and allow

the user to launch an activity to interact with the music player.

To request that your service run in the foreground, call

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),

System.currentTimeMillis());

Intent notificationIntent = new Intent(this, ExampleActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(this, getText(R.string.notification_title),

getText(R.string.notification_message), pendingIntent);

startForeground(ONGOING_NOTIFICATION, notification);

To remove the service from the foreground, call not stop the

service. However, if you stop the service while it's still running in the foreground, then the

notification is also removed.

Note: The methods setForeground() method—see the

For more information about notifications, see Creating Status Bar

Notifications.

Managing the Lifecycle of a Service

The lifecycle of a service is much simpler than that of an activity. However, it's even more important

that you pay close attention to how your service is created and destroyed, because a service

can run in the background without the user being aware.

The service lifecycle—from when it's created to when it's destroyed—can follow two

different paths:

A started service

The service is created when another component calls

A bound service

The service is created when another component (a client) calls not need to stop itself.)

These two paths are not entirely separate. That is, you can bind to a service that was already

started with

Implementing the lifecycle callbacks

Like an activity, a service has lifecycle callback methods that you can implement to monitor

changes in the service's state and perform work at the appropriate times. The following skeleton

service demonstrates each of the lifecycle methods:

61e8acd80ce3a664ab790b121a6616db.png

Figure 2. The service lifecycle. The diagram on the left

shows the lifecycle when the service is created with

public class ExampleService extends Service {

int mStartMode; // indicates how to behave if the service is killed

IBinder mBinder; // interface for clients that bind

boolean mAllowRebind; // indicates whether onRebind should be used

@Override

public void mStartMode;

}

@Override

public IBinder mBinder;

}

@Override

public boolean mAllowRebind;

}

@Override

public void

Note: Unlike the activity lifecycle callback methods, you are

not required to call the superclass implementation of these callback methods.

By implementing these methods, you can monitor two nested loops of the service's lifecycle:

The entire lifetime of a service happens between the time

The

The active lifetime of a service begins with a call to either

If the service is started, the active lifetime ends the same time that the entire lifetime

ends (the service is still active even after

Note: Although a started service is stopped by a call to

either onStop() callback). So, unless the service is bound to a client,

the system destroys it when the service is stopped—

Figure 2 illustrates the typical callback methods for a service. Although the figure separates

services that are created by

For more information about creating a service that provides binding, see the Bound Services document,

which includes more information about the Managing the Lifecycle of

a Bound Service.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值