开始binder调用service 最基本的几个步骤,具体参数查看SDK,最新SDK对参数要求不同,需要注意
第一步aidl
interface IMediaService { ......}第二步 service
public class xxxService extends Service {@Override public IBinder onBind(Intent intent) { return mBinder; } private class ServerStub extends IxxxService.Stub { @Override ...... } private final IBinder mBinder = new ServerStub(); }
第三步 connection
ServiceConnection为一个接口,用于绑定和解绑定IBinder,创建一个类实现它(或者new对象实现)
class XxxServiceConnection implements ServiceConnection{@Override public void onServiceDisconnected(ComponentName name) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { mService = Ixxx.Stub.asInterface(service); }}
第四步 bindService()
传给它ServiceConnection的实现public void connectService() { Intent intent = new Intent(...); mContext.bindService(...); } 用完断开 public void disConnectService() { mContext.unbindService(...); mContext.stopService(...); }