android.app.Service

android.app.Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding declaration in its package's AndroidManifest.xml. Services can be started with Context.startService() and Context.bindService(). (Ref: Service | Android Developers)

This exercise is a simple app with a dummy Local Service. Through the exercise, what can be noted are the life-cycle of a Service, how to link with Service with Start Service, Stop Service, Bind Service and Unbind Service, also the steps to implement the app with Service.

android.app.Service

Create a New Android Application as normal, with the main activity AndroidService.

Create a new class MyService extends Service. It's our service.
package com.exercise.AndroidService;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Toast.makeText(this,
"-MyService.onBind()-", Toast.LENGTH_LONG).show();
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(this,
"-MyService.onCreate()-", Toast.LENGTH_LONG).show();
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
Toast.makeText(this,
"-MyService.onDestroy()-", Toast.LENGTH_LONG).show();
}

@Override
public void onRebind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this,
"-MyService.onRebind()-", Toast.LENGTH_LONG).show();
}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this,
"-MyService.onStart()-", Toast.LENGTH_LONG).show();
}

@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this,
"-MyService.onUnbind()-", Toast.LENGTH_LONG).show();
return false;
}

public class LocalBinder extends Binder{
MyService getService(){
return MyService.this;
}
}

}


Modify main.xml to have four button to involve our expected task, Start Service, Stop Service, Bind Service and Unbind Service.
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/startservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Service"
/>
<Button
android:id="@+id/stopservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop Service"
/>
<Button
android:id="@+id/bindservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Bind Service"
/>
<Button
android:id="@+id/unbindservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Unbind Service"
/>
</LinearLayout>


Modify the main activity, AndroidService.java.
package com.exercise.AndroidService;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidService extends Activity {

private MyService myService;
private boolean serviceIsBinding;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button buttonStartService = (Button)findViewById(R.id.startservice);
Button buttonStopService = (Button)findViewById(R.id.stopservice);
Button buttonBindService = (Button)findViewById(R.id.bindservice);
Button buttonUnbindService = (Button)findViewById(R.id.unbindservice);

buttonStartService.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startService();
Toast.makeText(AndroidService.this,
"Start Service", Toast.LENGTH_LONG).show();
}});

buttonStopService.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopService();
Toast.makeText(AndroidService.this,
"Stop Service", Toast.LENGTH_LONG).show();
}});

buttonBindService.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bindService();
Toast.makeText(AndroidService.this,
"Bind Service", Toast.LENGTH_LONG).show();
}});

buttonUnbindService.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
unbindService();
Toast.makeText(AndroidService.this,
"Unbind Service", Toast.LENGTH_LONG).show();
}});
}

private ServiceConnection serviceConnection = new ServiceConnection(){

@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
myService = ((MyService.LocalBinder)arg1).getService();
Toast.makeText(AndroidService.this,
"onServiceConnected()", Toast.LENGTH_LONG).show();
}

@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
myService = null;
Toast.makeText(AndroidService.this,
"onServiceDisconnected()", Toast.LENGTH_LONG).show();
}};

private void startService(){
Intent intentService = new Intent(this, MyService.class);
this.startService(intentService);
}

private void stopService(){
Intent intentService = new Intent(this, MyService.class);
this.stopService(intentService);
}

private void bindService(){
Intent intentService = new Intent(this, MyService.class);
bindService(intentService, serviceConnection, Context.BIND_AUTO_CREATE);
serviceIsBinding = true;
}

private void unbindService(){
if(serviceIsBinding){
unbindService(serviceConnection);
serviceIsBinding = false;
}
}
}

In with, ServiceConnection is the interface for monitoring the state of an application service.

Finally, modify AndroidManifest.xml to add the <service> element.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidService"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidService"
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:enabled="true" android:name="MyService"></service>
</application>
<uses-sdk android:minSdkVersion="7" />

</manifest>


Download the files.