This post describes how to use Android Service. This is a key component in developing an Android app. Differently, from Activity, Service in Android runs in the background and they don’t have an interface and have a life-cycle very different from Activities.
Moreover, using a Service in Android we can implement multitask in Android.
Android Service Overview
We already know the lifecycle of an Android Activity; it can be started, stopped, destroyed if the system resources become too low and maybe can be recreated. On the contrary, a service in Android is designed to have a longer life.
It is important to notice that using Android Service we do not create automatically new threads, so if we implement a simple logic inside our Service, that doesn’t require long time processing, we don’t need to run it a separate thread. If we have to implement complex logic, with longtime processing, we have to take care of creating a new thread, otherwise, the Android service runs on the main thread and it could cause ANR problem.
In service in Android is used for two main reasons:
- Implement multi-task
- Enable Inter-Process-Communication (IPC)
A typical example of the first case is an app that required to download data from a remote server. In this case, we can have Activity that interacts with a user and starts a service that accomplishes the work in the background while the user can use the app. When the Android service finishes, it sends a message to the user.
In the second case, we want to “share” some common functions so that different app can re-use them. For example, we can suppose we have an Android Service that sends an email and we want to share this service with several apps so that we don’t have to rewrite the same code. In this case, we can use IPC so that the service exposes a “remote” interface that can be called by other apps.
In this post, we cover the first case: we have a local service, local means that the service can be seen just inside our APK.
Getting started with Android Service
Now we know more about Android Service, we want to create it. In Android to create a Service, we have to extend Service class.
[java]public class TestService extends Service {@Override
public IBinder onBind(Intent arg0) {
return null;
}
}[/java]
As we can see, we have to implement only one method called onBind. In our case, we are using local service, so this method should return null. As we mentioned before, an Android Service has its lifecycle and we can override some callback methods so that we can handle its different states:
[java]public class TestService extends Service {@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
[/java]
The first method onCreate is called only one time when the Service has to be created. If the Android Service is already running this method won’t be called. We don’t call it directly but it is the OS that calls it.
OnStartCommand is the most important method because it is called when we require starting the Service. In this method, we have the Intent passed at time we run the Service. In this way we can exchange some information with the Service. In this method, we implement our logic that can be executed directly inside this method if it isn’t time expensive otherwise we can create a thread. As you can see this method requires we return an Integer as result. This integer represents how the Service should be handled by the OS:
- START_STICKY : Using this return value, if the OS kills our Service it will recreate it but the Intent that was sent to the Service isn’t redelivered. In this way the Service is always running
- START_NOT_STICKY: If the SO kills the Service it won’t recreate it until the client calls explicitly onStart command
- START_REDELIVER_INTENT: It is similar to the START_STICKY and in this case, the Intent will be redelivered to the service.
OnDestroy is the method called by the OS when the Service will be destroyed.
Once we have our service class, we have to declare it in the Manifest.xml so that we can use it:
<service android:name=".TestService" android:enabled="true"/>
How to Start and Stop Service in Android
As we know a Service has to be started and eventually stopped so that it can accomplish its task. So the question is: How to start an Android service?
We can suppose to start it from an activity and we could pass to the service some information using Intent. We can suppose that our Activity has two buttons one to start and one to stop the Service:
[java]btnStart.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, TestService.class);
i.putExtra(“name”, “SurvivingwithAndroid”);
MainActivity.this.startService(i);
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, TestService.class);
MainActivity.this.stopService(i);
}
});[/java]
In the code above at line 5, we create an Intent passing the class name that handles our Service, moreover, we set some params like name and then we start the Service at line 7. In the same way, at line 17 we stop the service.
Clicking on Start button we get in the Log:
We can notice that the onCreate method is called because it is the first time we start the service if we click again on start button the OS doesn’t call onCreate method. When we click on stop button the OS destroy the service.
Android IntentService
As we mentioned before a service runs on the main thread, so we have to be very careful when we implement some logic in this service. We have to consider that if this logic is a blocking operation or it requires long time to finish a ANR problem could occur. In this case, we have to move our logic to a separate thread, meaning we have to create a thread in onStartCommand method and run it.
There is another class called Android IntentService derived from Service that simplifies our life. This class is useful when we don’t need to handle multiple requests at the same time. This class creates a worker thread to handle different requests. This class performs this operation:
- create a separate thread to handle the request
- create a request queue and pass one Intent at time
- create a default implementation of onStartCommand
- stop the service when all the requests are processed
If we want to create a IntentService we have to extend IntentService class instead of Service:
[java]public class TestIntentService extends IntentService {public TestIntentService() {
super(“TestIntentService”);
}
@Override
protected void onHandleIntent(Intent intent) {
}
}[/java]
In this case, we have only one method to implement called onHandleIntent. Here we implement out logic without caring if this is operation requires long time or not, because this method is called in a separate thread.
Starting an Android Service automatically at the boot time
Many times we want to start our service automatically, for example at boot time. We know to start a Service we need a component to start it. How can we do it? Well, we could use a Broadcast receiver that starts our service. If for example, we want to start it at the smartphone boot time, we create first a Broadcast receiver that listens to this event and then it starts the service.
[java]public class BootBroadcast extends BroadcastReceiver {@Override
public void onReceive(Context ctx, Intent intent) {
ctx.startService(new Intent(ctx, TestService.class));
}
}[/java]
and in the Manifest.xml
[xml]<receiver android:name=”.BootBroadcast”><intent-filter
action android:name=”android.intent.action.BOOT_COMPLETED”/>
</intent-filter>
</receiver>
[/xml]
At the end of this post, you hopefully gained the knowledge how to use Android Service and how to call services from an android app.
Android service is a component that is used to perform operations on the background such as playing music, handle network transactions, interacting content providers etc. It doesn’t has any…for more visit here http://thatsjavainfo.com/android-tutorials/android-service/
Your tutorials are very useful, thanks so much..
Very nice and well explained tutorials..
An extremely simple example that is barely informative…
Thank you for this wonderful information looking forward for more. I really appreciate your efforts to write such amazing piece of content for us.