Tuesday, January 22, 2013

Android Services Tutorial and Example


A service is an Android component that runs in the background without any user interaction.
It can be started and stopped by any component.While it is running, any component
can bind to it,












src/com/jitesh/servicesdemo/SimpleActivity.java

package com.jitesh.servicesdemo;


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

public class SimpleActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button startButton = (Button) findViewById(R.id.Button01);
        startButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                startService(new Intent(SimpleActivity.this, SimpleService.class));
            }
        });      

        Button stopButton = (Button)findViewById(R.id.Button02);
        stopButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                stopService(new Intent(SimpleActivity.this,SimpleService.class));
            }                
        });
    }
}




src/com/jitesh/servicesdemo/SimpleService.java


package com.jitesh.servicesdemo;

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

public class SimpleService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}

boolean paused = false;

@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service created ...", Toast.LENGTH_LONG).show();
paused = false;
Thread initBkgdThread = new Thread(new Runnable() {
public void run() {
play_music();
}
});
initBkgdThread.start();
}

@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
paused = true;
}

int[] notes = { R.raw.c5, R.raw.b4, R.raw.a4, R.raw.g4 };
int NOTE_DURATION = 400; // millisec
MediaPlayer m_mediaPlayer;

private void play_music() {
for (int ii = 0; ii < 12; ii++) {
// check to ensure main activity not paused
if (!paused) {
if (m_mediaPlayer != null) {
m_mediaPlayer.release();
}
m_mediaPlayer = MediaPlayer.create(this, notes[ii % 4]);
m_mediaPlayer.start();
try {
Thread.sleep(NOTE_DURATION);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

AndroidManifest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jitesh.servicesdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SimpleActivity"
            android:label="@string/title_activity_simple" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
          <service android:name=".SimpleService"></service>
    </application>

</manifest>


res/layout/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" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Do it" >
    </Button>

    <Button
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop it" >
    </Button>

</LinearLayout>

create a raw inside res and put some *RTTL files and give their name accordingly used in  program.


No comments:

Post a Comment