MainActivity.java
import android.content.Intent;
import android.app.*;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
    Button enableButton,disableButton;
    @Override
   protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);     enableButton=(Button)findViewById(R.id.button1);    disableButton=(Button)findViewById(R.id.button2);    enableButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startService(new Intent(MainActivity.this,
MyService.class));
}
});      disableButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
stopService(new Intent(MainActivity.this, MyService.class));
}
});
    }
}
MyService.java
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import android.media.*;
public class MyService extends Service {
MediaPlayer n;
    public MyService() {
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
    @Override
    public void onCreate() {
n= MediaPlayer.create(this,R.raw.owl);
        Toast.makeText(this, "The new Service was Created",
   Toast.LENGTH_LONG).show();
   }
    @Override
    public void onStart(Intent intent, int startId) {
        // For time consuming an long tasks you can launch a new thread here...
n.start();     
Toast.makeText(this, " Service Started",    Toast.LENGTH_LONG).show();
    }
    @Override
    public void onDestroy() {
n.stop();
        Toast.makeText(this, "Service Destroyed", 
Toast.LENGTH_LONG).show();
    }
}
main.xml
<LinearLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_margin="5dp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textColor="#ffffff"
android:text="Stop Service" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_margin="5dp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textColor="#ffffff"
android:text="Start Service" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.myapp3" >
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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:name="MyService"
            android:label="Service"/>
    </application>
</manifest>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
