bindService用于绑定一个服务。这样当bindService(intent,conn,flags)后,就会绑定一个服务。这样做可以获得这个服务对象本身,而用startService(intent)的方法只能启动服务。
bindService方式的一般过程:
①新建Service类BindService。在BindService类里新建内部类MyBinder,继承自Binder(Binder实现IBinder接口)。MyBinder提供方法返回BindService实例。
public class MyBinder extends Binder{
public BindService getService(){
return BindService.this;
}
}
实例化MyBinder得到mybinder对象;
重写onBind()方法: