B1: Đầu tiên là tạo 1 giao diện điều khiển service chơi nhạc bằng layout đơn giản - local_service_binding.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="4dip"
android:text="@string/local_service_binding"/>
<Button android:id="@+id/bind"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/bind_service">
<requestFocus />
</Button>
<Button android:id="@+id/unbind"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/unbind_service">
</Button>
<Button android:id="@+id/play"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/Play">
</Button>
</LinearLayout>
B2: Tạo mới localsample.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="4dip"
android:text="@string/local_sample"/>
<Button android:id="@+id/go"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/go">
<requestFocus />
</Button>
</LinearLayout>
B3: Chỉnh sửa value String:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">VD 5: AudioPro</string>
<string name="activity_local_service_binding">App/Service/Local Service Binding</string>
<string name="local_service_binding">Service Testing</string>
<string name="bind_service">Bind Service</string>
<string name="unbind_service">UnBind Service</string>
<string name="Play">Play</string>
<string name="local_sample">Service testing</string>
<string name="local_service_connected">Connected to local service</string>
<string name="local_service_disconnected">Disconnected from local service</string>
<string name="go">Go</string>
<string name="local_service_started">Local service has started</string>
<string name="local_service_stopped">Local service has stopped</string>
<string name="local_service_label">Sample Local Service</string>
<string name="activity_local_service_controller">App/Service/Local Service Controller</string>
<string name="local_service_controller">Service testing</string>
<string name="start_service">Play </string>
<string name="stop_service">Stop </string>
</resources>
B4: Tạo 1 class LocalService.java extend từ Service và một lớp con LocalBinder thừa kế từ lớp Binder (dùng để điều khiển service )
package com.developerlife.myservice;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
public class LocalService extends Service {
private NotificationManager mNM;
//Nạp chồng phương thức onBind bằng cách trả lại giá trị mBinder
private final IBinder mBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
//Tạo một đối tượng MediaPlayer chơi nhạc đơn giản ( sử dụng để chơi file abc.mp3 đặt trong folder res/raw )
MediaPlayer mMediaPlayer;
public void startMp3Player() {
mMediaPlayer = MediaPlayer.create(getApplicationContext(),
R.raw.abc);
mMediaPlayer.start();
}
public void mp3Stop() {
mMediaPlayer.stop();
mMediaPlayer.release();
}
public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}
@Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
@Override
public void onDestroy() {
mNM.cancel(R.string.local_service_started);
Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
}
}
B5: Ở lớp LocalServiceBinding.java extend từ lớp Activity chúng ta chỉ cần để ý đến đối tượng mConnection có nhiệm vụ giám sát kết nối của service chơi nhạc.
package com.developerlife.myservice;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class LocalServiceBinding extends Activity {
private boolean mIsBound;
private LocalService mBoundService;
Button mPlayButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.local_service_binding);
Button button = (Button)findViewById(R.id.bind);
button.setOnClickListener(mBindListener);
button = (Button)findViewById(R.id.unbind);
button.setOnClickListener(mUnbindListener);
mPlayButton = (Button)findViewById(R.id.play);
mPlayButton.setOnClickListener(mPlayListener);
mPlayButton.setText("Play");
mPlayButton.setEnabled(false);
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((LocalService.LocalBinder)service).getService();
Toast.makeText(LocalServiceBinding.this, R.string.local_service_connected,
Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
Toast.makeText(LocalServiceBinding.this, R.string.local_service_disconnected,
Toast.LENGTH_SHORT).show();
}
};
//Xử lý sự kiện 3 button ( Bind, Unbin, Play/Stop )
private OnClickListener mBindListener = new OnClickListener() {
public void onClick(View v) {
bindService(new Intent(LocalServiceBinding.this,
LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
mPlayButton.setEnabled(true);
}
};
private OnClickListener mPlayListener = new OnClickListener() {
public void onClick(View v) {
if(mPlayButton.getText() == "Play")
{
mBoundService.startMp3Player();
mPlayButton.setText("Stop");
}
else
{
mBoundService.mp3Stop();
mPlayButton.setText("Play");
}
}
};
private OnClickListener mUnbindListener = new OnClickListener() {
public void onClick(View v) {
if (mIsBound) {
unbindService(mConnection);
mIsBound = false;
mPlayButton.setEnabled(false);
}
}
};
}
Bây giờ Run Application để xem sản phẩm: