Tuesday, 3 January 2012

Running Background Service in android

This is example how to running the Service in the Background.

ServicesActivity.java


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 ServicesActivity extends Activity implements OnClickListener {
  private static final String TAG = "ServicesActivity";
  Button buttonStart, buttonStop;

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

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

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

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


service.java



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 service extends Service {
    private static final String TAG = "service";
    MediaPlayer player;
   
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
   
    @Override
    public void onCreate() {
           
        player = MediaPlayer.create(this, R.raw.sound);
        player.setLooping(false);
    }

    @Override
    public void onDestroy() {
       
        player.stop();
    }
   
    @Override
    public void onStart(Intent intent, int start) {
       
        player.start();
    }
}

main.xml

<?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"
    android:gravity="center">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:text="Service"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:id="@+id/start" android:text="Start"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:id="@+id/stop" android:text="Stop"></Button>
</LinearLayout>




AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.name" android:versionCode="1" android:versionName="1.0">
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".ServicesActivity" 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=".service" />
  </application>
</manifest>

No comments:

Post a Comment