Service is a fundamental component in android. A service is an application in Android that runs in the background without needing to interact with the user. These background processes need to keep running even when the phone is being used for other activities / tasks. Services are also ideal for situations in which there is no need to present a UI to the user.
For example, while using an application, you may want to play some background music at the same time. In this case, the code that is playing the background music has no need to interact with the user, and hence it can be run as a service.
The best way to understand how a service works is by creating one.
Step by Step we will create a simple service application.
Step 1: Using Eclipse, create a new Android project and name it as “Service DemoController”.
Step 2: Add a new class file to the project and name it NewService.java. Populate it with the following code:
package com.example.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class NewService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"Service created", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed", Toast.LENGTH_LONG).show();
}
}
First, we defined a class that extends the Service base class. All services extend the Service class:
public class NewService extends Service {
}
Within the NewService class, you implemented three methods:
@Override
public IBinder onBind(Intent arg0) {...}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {...}
@Override
public void onDestroy() {...}
The onBind() method enables you to bind an activity to a service. This in turn enables an activity to directly access members and methods inside a service which simply return a null for this method.
The onStartCommand() method is called when you start the service explicitly using the startService() method . This method signifies the start of the service, and you code it to do the things you need to do for your service. In this method, you returned the constant START_STICKY so that the service will continue to run until it is explicitly stopped.
The onDestroy() method is called when the service is stopped using the stopService() method. This is where you clean up the resources used by your service.
Step 3: All services that you have created must be declared in the AndroidManifest.xml file, like this:
<service android:name=”.NewService” />
Modify the Manifest.xml as below.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.service" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ServiceDemoControllerActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".NewService"/> </application> </manifest>
Step 4: In the main.xml file, add the following code:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/StartService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text ="Start Service"/> <Button android:id="@+id/StopService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Stop Service" /> </LinearLayout>
Step 5: Add the following code to MainActivity.Java as shown below.
package com.example.service;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ServiceDemoControllerActivity extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button start = (Button)findViewById(R.id.StartService);
Button stop = (Button)findViewById(R.id.StopService);
start.setOnClickListener(startListener);
stop.setOnClickListener(stopListener);
}
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v){
startService(new Intent(getBaseContext(), NewService.class));
}
};
private OnClickListener stopListener = new OnClickListener() {
public void onClick(View v){
stopService(new Intent(getBaseContext(), NewService.class));
}
};
}
To start a service, you use the startService() method, like this:
startService(new Intent(getBaseContext(), MyService.class));
To stop a service, use the stopService() method, like this:
stopService(new Intent(getBaseContext(), MyService.class));
Step 6: Press F11 to debug the application on the Android Emulator.
Step 7: Clicking the Start Service button will start the service as shown in fig. To stop the service, click the Stop Service button.
That’s it.



Recent Comments