1.在main.xml中加入如下四个按钮
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/button1"
android:text="开启音乐播放服务"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/button2"
android:text="停止音乐播放服务"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/button3"
android:text="绑定音乐播放服务"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/button4"
android:text="解绑定音乐播放服务"/>
2.在res目录下新建raw目录,并拷入mp3文件,我的是www.mp3
创建如下Service
public class HelloService extends Service {
MediaPlayer mp;//定义音乐播放器变量
private final static String TAG = "HelloService";
@Override
//绑定服务 通过 bindService 方法启动服务时调用
public IBinder onBind(Intent intent) {
Toast.makeText(this, "onBind(Intent intent)",Toast.LENGTH_SHORT).show();
mp.start();
return null;
}
@Override
//解绑定服务 通过 unbindService 方法解绑定服务时调用
public boolean onUnbind(Intent intent) {
Toast.makeText(this, "onUnbind(Intent intent)",Toast.LENGTH_SHORT).show();
mp.stop();
return false;
}
@Override
//创建服务 该服务不存在需要被创建时被调用,不管startService()还是bindService()都会在启动时调用该方法
public void onCreate() {
Toast.makeText(this, "onCreate()",Toast.LENGTH_SHORT).show();
//创建一个音乐播放器对象
mp = MediaPlayer.create(this.getApplicationContext(), R.raw.www);
//设置可以重复播放
mp.setLooping(true);
}
@Override
//停止服务服务 通过 onDestroy 方法停止服务时调用
public void onDestroy() {
Toast.makeText(this, "onDestroy()",Toast.LENGTH_SHORT).show();
mp.stop();
}
@Override
//开始服务 通过 onStart 方法开始服务时调用
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "onStart",Toast.LENGTH_SHORT);
mp.start();
}
}
3.在AndroidManifest.xml文件中注册Service
<application android:icon="@drawable/icon" android:label="@string/app_name">
<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 -->
<service android:name=".service.HelloService"></service>
</application>
4.Activity中的代码
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取页面组件
Button button1 = (Button) this.findViewById(R.id.button1);
Button button2 = (Button) this.findViewById(R.id.button2);
Button button3 = (Button) this.findViewById(R.id.button3);
Button button4 = (Button) this.findViewById(R.id.button4);
// 定义服务链接对象 -要启动哪个服务
final ServiceConnection sc = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(MainActivity.this,
"ServiceConnection onServiceDisconnected",
Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(MainActivity.this,
"ServiceConnection onServiceConnected",
Toast.LENGTH_SHORT).show();
}
};
// 定义监听事件
OnClickListener ocl = new OnClickListener() {
//在意图中指定服务
Intent intent = new Intent(MainActivity.this, HelloService.class);
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
startService(intent);
break;
case R.id.button2:
stopService(intent);
break;
case R.id.button3:
// 绑定启动要指定服务链接对象,绑定的操作选项
bindService(intent, sc, Context.BIND_AUTO_CREATE);
break;
case R.id.button4:
unbindService(sc);
break;
}
}
};
//为按钮加点击事件
button1.setOnClickListener(ocl);
button2.setOnClickListener(ocl);
button3.setOnClickListener(ocl);
button4.setOnClickListener(ocl);
}
}
5.从这个例子中我们可以体会到Context.startService()方法和Context.bindService()方法的区别
在模拟器中点击
开启音乐播放服务后,播放音乐,此时点击右侧的回退按钮,音乐仍然播放。但是通过
绑定音乐播放服务启动的音乐,在点击回退按钮后,音乐也停止了播放,说明通过这种方式启动的服务是与Context绑定的。