Working with a Service for Android


This tutorial is intended to be a quick guide to get you up and running with basic Service knowledge for Android. Ideally, you can visit here for more in depth information.

Whats a service?

  • A Service is an application component that can perform long-running operations in the background, and it does not provide a user interface.
  • Another application component can start a service, and it continues to run in the background even if the user switches to another application.
  • Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC).

 

  • There’s 3 different types of services:
    1. Scheduled: A service is scheduled when an API such as the JobScheduler launches the service. You can use the JobScheduler by registering jobs and specifying their requirements for network and timing. The system then gracefully schedules the jobs for execution at the appropriate times.
    2. Started: A service is started when an application component (such as an activity) calls startService(). After it’s started, a service can run in the background indefinitely, even if the component that started it is destroyed.
    3. Bound: A service is bound when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, receive results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it.

What can I use a service for?

  • As stated already, you can use a service to perform background tasks. For example, a service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

How can I use a service?

To begin, all services must be declared in the AndroidManifest.xml file like so:

<!-- The android:name attribute must specify the same name as the class service -->
<service android:name=".MyService"/>

Next, create the class that will act as the service. You must extend Service class in order for your class to be considered a service.

public class MyService extends Service
{
     //classes that extend Service must implement this method
     //even if you don't use it, simply just return null
     @Override
     public IBinder onBind(Intent intent)
     {
         return null;
     }
     //rest of your implementation here...
}

You can start a service from any class like so:

Intent myServiceIntent = new Intent(this, MyService.class);
startService(myServiceIntent);

//you can also start a service by binding it like so:
//bindService(myServiceIntent);

You can, if you want, end a Service like so (usually in the onStop() or onDestroy() methods):

stopService(myServiceIntent);
//or you can unbind the service
//unBindService(myServiceIntent);

 

The Android Service lifecycle.

Advertisements

One thought on “Working with a Service for Android

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s