当一个Activity通过Intent绑定一个Service时,会调用Service的onBind方法。此方法会返回一个IBinder对象,此对象作为参数传递给Activity中定义好的ServiceConnection中的onServiceConnected()方法,使得Activity获取到Service的操作接口
下面是一个例子,通过两个按钮来绑定和解绑一个Service,但是需要注意的是,如果当前没有绑定Service而直接解绑,会出现IllegalArgumentException,所以比较好的解决办法是使用一个标记接口判断绑定对象是否存在,具体见代码
标记接口:
// 我只是一个空接口 public interface IService {}
然后是我们自己的Service类:
1 public class MyService extends Service { 2 3 private MyBinder myBinder = new MyBinder(); 4 5 @Override 6 public IBinder onBind(Intent intent) { 7 System.out.println("**** onBind() ****"); 8 return myBinder; 9 } 10 11 @Override 12 public void onCreate() { 13 System.out.println("**** onCreate() ****"); 14 super.onCreate(); 15 } 16 17 @Override 18 public void onDestroy() { 19 System.out.println("**** onDestroy() ****"); 20 super.onDestroy(); 21 } 22 23 @Override 24 public int onStartCommand(Intent intent, int flags, int startId) { 25 System.out.println("**** onStartCommand() ****"); 26 return super.onStartCommand(intent, flags, startId); 27 } 28 29 @Override 30 public void onRebind(Intent intent) { 31 System.out.println("**** onRebind() ****"); 32 super.onRebind(intent); 33 } 34 35 @Override 36 public boolean onUnbind(Intent intent) { 37 System.out.println("**** onUnbind() ****"); 38 return super.onUnbind(intent); 39 } 40 41 // 注意! MyBinder为Binder与IService的共同子类 42 class MyBinder extends Binder implements IService {} 43 44 }
Activity:
1 public class MainActivity extends Activity { 2 3 private Button bindServiceBtn = null; 4 private Button unbindServiceBtn = null; 5 private IService myService = null; 6 7 // 实例化ServiceConnection接口用以处理绑定后Service传回的操作接口 8 private ServiceConnection conn = new ServiceConnection() { 9 10 @Override 11 public void onServiceConnected(ComponentName name, IBinder service) { 12 System.out.println("**** conn.onServiceConnected() ****"); 13 // 此处实际上是参数service向下转型为MyBinder, 然后再向上转型为IService,若绑定成功,则IService对象不为空 14 myService = (MyBinder) service; 15 } 16 17 @Override 18 public void onServiceDisconnected(ComponentName name) { 19 System.out.println("**** conn.onServiceDisconnected() ****"); 20 } 21 22 }; 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 29 bindServiceBtn = (Button) findViewById(R.id.bindServiceBtn); 30 unbindServiceBtn = (Button) findViewById(R.id.unbindServiceBtn); 31 32 bindServiceBtn.setOnClickListener(new BindServiceListener()); 33 unbindServiceBtn.setOnClickListener(new UnbindServiceListener()); 34 35 } 36 37 private class BindServiceListener implements OnClickListener { 38 @Override 39 public void onClick(View v) { 40 Intent intent = new Intent(MainActivity.this, MyService.class); 41 MainActivity.this.bindService(intent, conn, Context.BIND_AUTO_CREATE); 42 } 43 } 44 45 private class UnbindServiceListener implements OnClickListener { 46 @Override 47 public void onClick(View v) { 48 // 判断IService是否为空,不为空才可解绑 49 if (MainActivity.this.myService != null) { 50 MainActivity.this.unbindService(conn); 51 MainActivity.this.myService = null; 52 } else { 53 Toast.makeText(MainActivity.this, "请绑定服务", Toast.LENGTH_SHORT).show(); 54 } 55 } 56 } 57 58 }