一、绑定服务介绍
前面文章中讲过一般的通过startService开启的服务,当访问者关闭时,服务仍然存在;
但是如果存在这样一种情况:访问者需要与服务进行通信,则我们需要将访问者与服务进行绑定;
如果使用Context.bindService()方法启动服务,则在服务未创建时,系统会调用服务的onCreate()方法,接着调用onBind()方法,这时就访问者与服务已经绑定;
如果多次调用bindService()多次绑定服务,则不会有副作用(导致多次绑定);
绑定服务的运行流程如下图所示:
二、Bind Service 核心代码
绑定与解绑定服务
(1)Context.bindService(Intent service,ServiceConnection conn,BIND_AUTO_CREATE);//绑定服务
(2)Context.unbindService(ServiceConnection conn); //解除绑定服务
ServiceConnection
ServiceConnection为一个接口,用于绑定和解绑定IBinder,因此需要创建一个类实现它;
class XxxServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) { //service为在onBind返回的IBinder
//绑定Binder对象
}
@Override
public void onServiceDisconnected(ComponentName name) {
//解绑定Binder对象
}
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) { //service为在onBind返回的IBinder
//绑定Binder对象
}
@Override
public void onServiceDisconnected(ComponentName name) {
//解绑定Binder对象
}
}
Service类
class XxxService extends Service{
private IBinder binder = new
XxxBinder();
public IBinder onBind(Intent intent){
return binder;
}
public int fun(int a){
//服务提供的方法,但是不能直接调用
...
}
private class XxxBinder extends Binder implements IXxxBinder{
//面向接口编程
public return fun1(int a){
//对外暴露的API
return fun(a);
//内部调用Service的方法
}
}
}
三、BindService案例
场景:服务提供了加法运算的接口,Activity需要调用服务提供的加法运算方法,并返回结果;
效果说明:
MainActivity.java
package com.xiazdong.bindservice; import android.app.Activity; 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; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText op1,op2; private Button button; private TextView result; private IAddOp binder ;//使用接口IAddOp private ServiceConnection conn = new AddOpServiceConnection(); private OnClickListener listener = new OnClickListener(){ @Override public void onClick(View v) { int number = binder.addOpService(Integer.parseInt(op1.getText().toString()), Integer.parseInt(op1.getText().toString())); result.setText("result="+number+""); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); op1 = (EditText)this.findViewById(R.id.op1); op2 = (EditText)this.findViewById(R.id.op2); result = (TextView)this.findViewById(R.id.result); button = (Button)this.findViewById(R.id.button); button.setOnClickListener(listener); Intent service = new Intent(this,AddOpService.class); this.bindService(service, conn, BIND_AUTO_CREATE); } private class AddOpServiceConnection implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) {//绑定服务时调用 binder = (IAddOp)service; } @Override public void onServiceDisconnected(ComponentName name) {//解绑定服务时调用 binder = null; } } }
AddOpService.java
package com.xiazdong.bindservice; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class AddOpService extends Service { private IBinder binder = new AddOpBinder(); public int add(int a,int b){ //服务提供的方法 return a+b; } @Override public IBinder onBind(Intent intent) { return binder; } private class AddOpBinder extends Binder implements IAddOp{ public int addOpService(int a, int b) { //binder对外暴露一个API调用服务提供的方法 return add(a,b); } } }
IAddOpService.java
package com.xiazdong.bindservice; public interface IAddOp { public int addOpService(int a,int b); }