Use Of Services

This is a sample activity which shows how a Service works? A service is an application component that can perform long running operations in background and does not provide a user interface.

Underlying Algorithm:

Basic description of algorithm in step by step form:
1.) Create a Project Services
2.) Create a class MyService which extends Service.

 

public class MyService extends Service

 

 

3.) Since MyService extends Service class, it is necessary to override various methods of its lifecycle ie onCreate(), onStart(), or onDestroy().
4.) To begin a service in the application you need to call startService() which invokes the service onCreate() method and onStart() to begin running the service.
5.) Call the application’s stopService() method to stop the service.
6.) Method onBind(Intent intent) returns the communication channel to the service. It may return null if client cannot bind to the service.

 

public IBinder onBind(Intent intent) {
return null;
}

 

 

7.) ServiceSampleActivity plays an audio file and by click events of the buttons invokes the MyService service.
8.) Every service needs to be declared in AndroidManifest.xml:

 

<!--?xml version="1.0" encoding="utf-8"?-->
package="com.sample.ServiceSample" android:versionCode="1" android:versionName="1.0">

 

 

9.) For Creating the table Layout put the following in main.xml in layout:

 

<!--?xml version="1.0" encoding="utf-8"?-->
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">

<TableLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:stretchColumns="*" >

<button></button>
<button></button>

 

 

10.) Run the application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. Services. Enter following information:
Project name: Services
Build Target: Android 2.3
Application name: ServiceSample
Package name: com.sample.ServiceSample
Create Activity: ServiceSampleActivity

On Clicking Finish Services code structure is generated with the necessary Android Packages being imported along with ServiceSampleActivity.java. ServiceSampleActivity class will look like following:

 

package com.sample.ServiceSample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ServiceSampleActivity extends Activity implements OnClickListener {
private static final String TAG = "ServicesDemo";
Button buttonStart, buttonStop;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);

buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}

public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
stopService(new Intent(this, MyService.class));
break;
}
}
}

 

 

2.) MyService class will look like following:

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
private static final String TAG = "Service Demo";
MediaPlayer player;

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
Toast.makeText(this, "Service Created...", Toast.LENGTH_LONG).show();

player = MediaPlayer.create(this, R.raw.preview);
player.setLooping(false); // Set looping
}

@Override
public void onDestroy() {
Toast.makeText(this, "Service Stopped..", Toast.LENGTH_LONG).show();

player.stop();
}

@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Service Started...", Toast.LENGTH_LONG).show();

player.start();
}
}

 

 

3.) Run the application.

Output –The final output:



It will play Background Music.

And by clicking on Stop Service will stop the music.