bindService():允许其他组件跟它进行通信,允许多个客户端绑定到同一个service上,当所有的客户端都解除绑定后,该service就销毁了。
1、main.xml包括bindservice和unbindservice2个按钮,还有模拟的四个按钮
<?xml version="1.0" encoding="utf-8"?>
<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/bind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BindService" />
<Button
android:id="@+id/play"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放" />
<Button
android:id="@+id/pause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="暂停" />
<Button
android:id="@+id/next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下一首" />
<Button
android:id="@+id/pervious"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="上一首" />
<Button
android:id="@+id/unbind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="UnBindService" />
</LinearLayout>
2、AndroidMainfest.xml声明Service
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.huangyi.bindservicedemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<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="com.huangyi.bindservicedemo.BindService"/>
</application>
</manifest>
3、BindService.java里面定义了bindService生命周期的几个方法onCreate----->onBind--->onUnbind---->onDestroy,还自定义了一个类MyBinder集成Binder这个类,并在这个类里面写一个方法返回BindService类的对象。当然还有模拟播放的几个方法。
package com.huangyi.bindservicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class BindService extends Service {
@Override
public void onCreate() {
Log.i("info", "BindService--->onCreate()");
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
Log.i("info", "BindService--->onBind()");
return new MyBinder();
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("info", "BindService--->onUnbind()");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.i("info", "BindService--->onDestroy()");
super.onDestroy();
}
public class MyBinder extends Binder{
public BindService getService(){
return BindService.this;
}
}
public void play(){
Log.i("info", "播放");
}
public void pause(){
Log.i("info", "暂停");
}
public void next(){
Log.i("info", "下一首");
}
public void pervious(){
Log.i("info", "上一首");
}
}
4、MainActivity.java
其中定义了ServiceConnection接口的实现对象conn,将传递过来的binder对象转化为BindService的对象。
package com.huangyi.bindservicedemo;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
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;
public class MainActivity extends Activity implements OnClickListener{
private Button bind,play,pause,next,pervious,unbind;
private Intent intent;
BindService service;
ServiceConnection conn = new ServiceConnection() {
/**
* 当启动源跟Service的连接意外丢失的时候会调用这个方法
* ,比如Service崩溃了或者被强行杀死了
*/
@Override
public void onServiceDisconnected(ComponentName name) {
}
/**
* 当启动源跟Service成功连接之后将会调用这个方法
*/
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
service = ((BindService.MyBinder)binder).getService();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bind = (Button) findViewById(R.id.bind);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
next = (Button) findViewById(R.id.next);
pervious = (Button) findViewById(R.id.pervious);
unbind = (Button) findViewById(R.id.unbind);
bind.setOnClickListener(this);
play.setOnClickListener(this);
pause.setOnClickListener(this);
next.setOnClickListener(this);
pervious.setOnClickListener(this);
unbind.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bind:
intent = new Intent(MainActivity.this, BindService.class);
bindService(intent, conn, Service.BIND_AUTO_CREATE);
break;
case R.id.play:
service.play();
break;
case R.id.pause:
service.pause();
break;
case R.id.next:
service.next();
break;
case R.id.pervious:
service.pervious();
break;
case R.id.unbind :
unbindService(conn);
break;
}
}
}
运行结果:
依次从上到下点击按钮
代码地址:http://download.csdn.net/detail/yihuangol/9259295