Eclipse Background Service (EBS)


EBS:   SOLD       




What is a background service for? Background service is one way for the system to run its functions without the user's knowledge. During the process, there is no interaction between the user and the application. The simplest implementation is the sound in a game, or the auto update function in certain applications.


Creating Android Background Service in Eclipse

Well, this time we learn the implementation of background service using sound. The idea is that in the application UI there are 2 buttons, namely start and stop. If the start button is clicked, the application will hear the sound of music. The music will continue to be heard even if you click the back or home button. Then how to stop it? Yes, you have to go back to this application and then click the stop button. Note Figure 8.1 is a display of a simple background service application.


Figure 8.1. Background service implementation using sound

Are you ready??

1. Create a new project

| Project name     | ServiceBackground |
|------------------|-------------------|
| Build Target     | Android 2.2       |
| Application name | ServiceSederhana  |
| Package name     | Com.serv.bg       |
| Create Activity  | ServiceSederhana  |
| Min SDK version  | 8                 |

2. Prepare strings.xml first

<?xml version="1.0" encoding="utf-8"?>
 <resources>
     <string name="hello">ServiceSederhana!</string>
     <string name="app_name">Service Background
         Sederhana</string>
     <string name="startBtn">Start</string>
     <string name="stopBtn">Stop</string>
</resources>

3. We make the layout in main.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical" >

     <TextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/hello"
   android:textSize="24dip" />

     <Button
         android:id="@+id/startBtn"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="@string/startBtn" >
  </Button>
     <Button
         android:id="@+id/stopBtn"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="@string/stopBtn" >
  </Button>
 </LinearLayout>

4. Create a raw folder inside the res folder. Just put the mp3 files in the raw folder.

5. Create a class MyService.java

package com.serv.bg;

 import android.app.Service;
 import android.content.Intent;
 import android.media.MediaPlayer;
 import android.os.IBinder;

 public class MyService extends Service{
 MediaPlayer mp;

 @Override
 public IBinder onBind(Intent arg0) {
 // TODO Auto-generated method stub
return null;
 }
 @Override
 public void onCreate(){
 mp=MediaPlayer.create(this, R.raw.beraksi);
 mp.setLooping(false);
 }

public void onStart(Intent intent,int startId){
 mp.start();
 }

 @Override
 public void onDestroy(){
 mp.stop();
 }
 }

6. Create a ServiceSederhana.java activity

package com.serv.bg;

 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 ServiceSederhana extends Activity implements
OnClickListener {
 Button startBtn,stopBtn;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 startBtn=(Button)findViewById(R.id.startBtn);
 stopBtn=(Button)findViewById(R.id.stopBtn);
 startBtn.setOnClickListener(this);
 stopBtn.setOnClickListener(this);

}
 @Override
 public void onClick(View v) {
 switch(v.getId()){
 case R.id.startBtn:
 startService(new Intent(this,MyService.class));
 break;
 case R.id.stopBtn:
 stopService(new Intent(this,MyService.class));
 break;
 }
 // TODO Auto-generated method stub

 }
}

Post a Comment

Previous Next

نموذج الاتصال