Login     Sign Up
Tugadar (@admin)
18 days ago
12 Views

Unlike Activities, which present a rich graphical interface to users, Services run in the background — updating your Content Providers, firing Intents, and triggering Notifications. They are the perfect way to perform regular processing or handle events even after your application’s Activities are invisible, inactive, or have been closed.

With no visual interface, Services are started, stopped, and controlled from other application compo-nents including other Services, Activities, and Broadcast Receivers. If your application regularly, or con-tinuously, performs actions that don’t depend directly on user input, Services may be the answer.

Started Services receive higher priority than inactive or invisible Activities, making them less likely to be terminated by the run time’s resource management. The only time Android will stop a Service pre-maturely is when it’s the only way for a foreground Activity to gain required resources; if that happens, your Service will be restarted automatically when resources become available.

Applications that update regularly but only rarely or intermittently need user interaction are good can-didates for implementation as Services. MP3 players and sports-score monitors are examples of applica-tions that should continue to run and update without an interactive visual component (Activity) visible.

Further examples can be found within the software stack itself; Android implements several Services including the Location Manager, Media Controller, and the Notification Manager.

Creating and Controlling Services

Services are designed to run in the background, so they need to be started, stopped, and controlled by other application components.

In the following sections, you’ll learn how to create a new Service, and how to start and stop it using Intents and the startService method. Later you’ll learn how to bind a Service to an Activity, provid-ing a richer interface for interactivity.

Creating a Service

To define a Service, create a new class that extends the Service base class. You’ll need to override onBind and onCreate, as shown in the following skeleton class:

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

public class MyService extends Service {

@Override

public void onCreate() {
// TODO: Actions to perform when service is created.

}

@Override

public IBinder onBind(Intent intent) {

TODO: Replace with service binding implementation. return null;

}

}

In most cases, you’ll also want to override onStart. This is called whenever the Service is started with a call to startService, so it can be executed several times within the Service’s lifetime. You should ensure that your Service accounts for this.

The snippet below shows the skeleton code for overriding the onStart method:

@Override

public void onStart(Intent intent, int startId) {

// TODO: Actions to perform when service is started.

}

Once you’ve constructed a new Service, you have to register it in the application manifest.

Do this by including a service tag within the application node. You can use attributes on the service tag to enable or disable the Service and specify any permissions required to access it from other applica-tions using a requires-permission flag.

Below is the service tag you’d add for the skeleton Service you created above:

<service android:enabled=”true” android:name=”.MyService”></service>