14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8: Working in the Background<br />

250<br />

Alarms provide a mechanism for fi ring Intents at set times, outside the control of your application life<br />

cycle. You’ll learn to use Alarms to start Services, open Activities, or broadcast Intents based on either<br />

the clock time or the time elapsed since device boot. An Alarm will fi re even after its owner application<br />

has been closed, and can (if required) wake a device from sleep.<br />

Introducing Services<br />

Unlike Activities, which present a rich graphical interface to users, Services run in the background —<br />

updating your Content Providers, fi ring Intents, and triggering Notifi cations. They are the perfect way<br />

to perform regular processing or handle events even after your application’s Activities are invisible,<br />

inactive, or have been closed.<br />

With no visual interface, Services are started, stopped, and controlled from other application components<br />

including other Services, Activities, and Broadcast Receivers. If your application regularly, or continuously,<br />

performs actions that don’t depend directly on user input, Services may be the answer.<br />

Started Services receive higher priority than inactive or invisible Activities, making them less likely to<br />

be terminated by the run time’s resource management. The only time Android will stop a Service prematurely<br />

is when it’s the only way for a foreground Activity to gain required resources; if that happens,<br />

your Service will be restarted automatically when resources become available.<br />

<strong>Application</strong>s that update regularly but only rarely or intermittently need user interaction are good candidates<br />

for implementation as Services. MP3 players and sports-score monitors are examples of applications<br />

that should continue to run and update without an interactive visual component (Activity) visible.<br />

Further examples can be found within the software stack itself; Android implements several Services<br />

including the Location Manager, Media Controller, and the Notifi cation Manager.<br />

Creating and Controlling Services<br />

Services are designed to run in the background, so they need to be started, stopped, and controlled by<br />

other application components.<br />

In the following sections, you’ll learn how to create a new Service, and how to start and stop it using<br />

Intents and the startService method. Later you’ll learn how to bind a Service to an Activity, providing<br />

a richer interface for interactivity.<br />

Creating a Service<br />

To defi ne a Service, create a new class that extends the Service base class. You’ll need to override<br />

onBind and onCreate, as shown in the following skeleton class:<br />

import android.app.Service;<br />

import android.content.Intent;<br />

import android.os.IBinder;<br />

public class MyService extends Service {<br />

@Override<br />

public void onCreate() {

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!