赞
踩
binder的原理和实现方法不过多赘述。我们这里主要是从代码的层面告诉你如何去看binder在代码中的使用
直接上代码
binderserver端的services代码。
public class BinderService extends Service{ private List<Dog> mDogsList = new ArrayList<Dog>(); private final IDogManager.DogManagerImpl mBinder = new IDogManager.DogManagerImpl() { @Override public List<Dog> getDogList() throws RemoteException { Log.d("qqq","getDogList services"); return mDogsList; } @Override public void attchinfo(IPersonManager iPersonManager) throws RemoteException { Log.d("qqq","attchinfo services"); iPersonManager.addPersonDog(new Dog()); } }; @Override public IBinder onBind(Intent intent) { Log.d("qqq"," services onBind"); return mBinder.asBinder(); } }
这是binderClient生成的代码
public class MainActivity extends Activity{ private IDogManager mService; private Ainfo mAinfo; public class Ainfo extends IPersonManager.Stub{ @Override public void addPersonDog(Dog dog){ Log.d("qqq","client addPersonDog"); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mAinfo = new Ainfo(); Intent intent = new Intent("com.example.binderservice.BinderService"); intent.setPackage("com.example.binderservice"); bindService(intent, sc, Context.BIND_AUTO_CREATE); } private ServiceConnection sc = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d("qqq","onServiceConnected"); mService = IDogManager.DogManagerImpl.asInterface(service); try { mService.attchinfo(mAinfo); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { mService = null; } }; }
这是binder代码,也是我们aidl生成的代码
public interface IDogManager extends IInterface { static final String DESCRIPTOR = "com.example.administrator.writebindercodeexample.IDogManager"; static final int TRANSACTION_getDogList = IBinder.FIRST_CALL_TRANSACTION + 0; static final int TRANSACTION_addDog = IBinder.FIRST_CALL_TRANSACTION + 1; static final int TRANSACTION_attachinfo = IBinder.FIRST_CALL_TRANSACTION + 2; public List<Dog> getDogList() throws RemoteException; public void attchinfo(IPersonManager iPersonManager) throws RemoteException; public abstract class DogManagerImpl extends Binder implements IDogManager { public DogManagerImpl() { Log.d("qqq", "DogManagerImpl"); this.attachInterface(this, DESCRIPTOR); } public static com.example.binderservice.IDogManager asInterface(android.os.IBinder obj) { Log.d("qqq", "asInterface"); if ((obj == null)) { return null; } android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); //如果是同1个进程,也就是说进程内通信的话 我们就返回括号内里的对象 if (((iin != null) && (iin instanceof com.example.binderservice.IDogManager))) { return ((com.example.binderservice.IDogManager) iin); } //如果不是同一进程,是2个进程之间相互通信,那我们就得返回这个Stub.Proxy 看上去叫Stub 代理的对象了 return new com.example.binderservice.IDogManager.DogManagerImpl.Proxy(obj); } @Override public IBinder asBinder() { return this; } @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { Log.d("qqq", "onTransact " + code); switch (code) { case INTERFACE_TRANSACTION: { reply.writeString(DESCRIPTOR); return true; } case TRANSACTION_getDogList: { data.enforceInterface(DESCRIPTOR); java.util.List<com.example.binderservice.Dog> _result = this.getDogList(); reply.writeNoException(); reply.writeTypedList(_result); return true; } case TRANSACTION_attachinfo: { data.enforceInterface(DESCRIPTOR); com.example.binderservice.IPersonManager _arg0; _arg0 = com.example.binderservice.IPersonManager.Stub.asInterface(data.readStrongBinder()); this.attchinfo(_arg0); reply.writeNoException(); return true; } } return super.onTransact(code, data, reply, flags); } private static class Proxy extends DogManagerImpl { private android.os.IBinder mRemote; Proxy(android.os.IBinder remote) { mRemote = remote; } @Override public android.os.IBinder asBinder() { Log.d("qqq", "asBinder"); return mRemote; } public java.lang.String getInterfaceDescriptor() { Log.d("qqq", "getInterfaceDescriptor"); return DESCRIPTOR; } @Override public java.util.List<com.example.binderservice.Dog> getDogList() throws android.os.RemoteException { Log.d("qqq", "getDogList"); android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); java.util.List<com.example.binderservice.Dog> _result; try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(DogManagerImpl.TRANSACTION_getDogList, _data, _reply, 0); _reply.readException(); _result = _reply.createTypedArrayList(com.example.binderservice.Dog.CREATOR); } finally { _reply.recycle(); _data.recycle(); } return _result; } @Override public void attchinfo(IPersonManager iPersonManager) throws RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); try { _data.writeInterfaceToken(DESCRIPTOR); _data.writeStrongBinder((iPersonManager != null) ? iPersonManager.asBinder() : null); mRemote.transact(DogManagerImpl.TRANSACTION_attachinfo, _data, _reply, 0); _reply.readException(); } finally { _reply.recycle(); _data.recycle(); } } } } }
那么代码的第一步在哪里呢?如何向serverManager注册呢
是`this.attachInterface(this, DESCRIPTOR);`
我们来找一下
private final IDogManager.DogManagerImpl mBinder = new IDogManager.DogManagerImpl() {
@Override
public List<Dog> getDogList() throws RemoteException {
Log.d("qqq","getDogList services");
return mDogsList;
}
@Override
public void attchinfo(IPersonManager iPersonManager) throws RemoteException {
Log.d("qqq","attchinfo services");
iPersonManager.addPersonDog(new Dog());
}
};
public abstract class DogManagerImpl extends Binder implements IDogManager {
public DogManagerImpl() {
Log.d("qqq", "DogManagerImpl");
this.attachInterface(this, DESCRIPTOR);
}
大家可以看到当我们new出来的IDogManager.DogManagerImpl mBinder
会走到父类的构造方法,这里也就是实现了第一步,向ServerManager注册。
所以也就可以这么说,只要是new出来的public abstract class A extends Binder implements IInterface
的对象,基本上都是默认实现了向ServerManager注册的(默认aidl是在构造方法中实现了this.attachInterface(this, DESCRIPTOR);
)。
关键代码是obj.queryLocalInterface(DESCRIPTOR);
private ServiceConnection sc = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d("qqq","onServiceConnected"); mService = IDogManager.DogManagerImpl.asInterface(service); try { mService.attchinfo(mAinfo); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { mService = null; } };
public static com.example.binderservice.IDogManager asInterface(android.os.IBinder obj) {
Log.d("qqq", "asInterface");
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
//如果是同1个进程,也就是说进程内通信的话 我们就返回括号内里的对象
if (((iin != null) && (iin instanceof com.example.binderservice.IDogManager))) {
return ((com.example.binderservice.IDogManager) iin);
}
//如果不是同一进程,是2个进程之间相互通信,那我们就得返回这个Stub.Proxy 看上去叫Stub 代理的对象了
return new com.example.binderservice.IDogManager.DogManagerImpl.Proxy(obj);
}
大家可以看到当我们bind service以后,会拿到IBinder service
,这个对象是bind上service后Services端返回的。
@Override
public IBinder onBind(Intent intent) {
Log.d("qqq"," services onBind");
return mBinder.asBinder();
}
大家看这行代码
mService = IDogManager.DogManagerImpl.asInterface(service);
这个asInterface就会走到android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
这就是第二步,Client向ServerManager查询binder服务。
接下来会拿到代理对象
return new com.example.binderservice.IDogManager.DogManagerImpl.Proxy(obj);
关键方法mRemote.transact
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("qqq","onServiceConnected");
mService = IDogManager.DogManagerImpl.asInterface(service);
try {
mService.attchinfo(mAinfo);
} catch (RemoteException e) {
e.printStackTrace();
}
}
这里会走到哪里呢?
@Override
public void attchinfo(IPersonManager iPersonManager) throws RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeStrongBinder((iPersonManager != null) ? iPersonManager.asBinder() : null);
mRemote.transact(DogManagerImpl.TRANSACTION_attachinfo, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
当代码走到mRemote.transact(DogManagerImpl.TRANSACTION_attachinfo, _data, _reply, 0);
表示我要把参数和传递给了ServerManager,请Services端进行计算。之后进程会休眠,等待结果。
休眠在中间
mRemote.transact(DogManagerImpl.TRANSACTION_attachinfo, _data, _reply, 0); _reply.readException();
_reply.readException();意思是有异常会返回到这里。
当然这么说是不正确的,你可以这么理解,这个方法是无参数返回的,放个有参数返回的方法。
@Override public java.util.List<com.example.binderservice.Dog> getDogList() throws android.os.RemoteException { Log.d("qqq", "getDogList"); android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); java.util.List<com.example.binderservice.Dog> _result; try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(DogManagerImpl.TRANSACTION_getDogList, _data, _reply, 0); _reply.readException(); _result = _reply.createTypedArrayList(com.example.binderservice.Dog.CREATOR); } finally { _reply.recycle(); _data.recycle(); } return _result; }
_reply.readException();
_result = _reply.createTypedArrayList(com.example.binderservice.Dog.CREATOR);
这个_result是返回结果,最后return就返回到你调用的地方。
关键方法ontransact
@Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { Log.d("qqq", "onTransact " + code); switch (code) { case INTERFACE_TRANSACTION: { reply.writeString(DESCRIPTOR); return true; } case TRANSACTION_getDogList: { data.enforceInterface(DESCRIPTOR); java.util.List<com.example.binderservice.Dog> _result = this.getDogList(); reply.writeNoException(); reply.writeTypedList(_result); return true; } case TRANSACTION_attachinfo: { data.enforceInterface(DESCRIPTOR); com.example.binderservice.IPersonManager _arg0; _arg0 = com.example.binderservice.IPersonManager.Stub.asInterface(data.readStrongBinder()); this.attchinfo(_arg0); reply.writeNoException(); return true; } } return super.onTransact(code, data, reply, flags); }
经过一系列调用,代码就会走到service端的onTransact方法,这里会根据你之前Client传的参数,判断你要调取的方法,进行调取。
这里的_arg0就是参数(这里是我使用了双向binder通讯,所以参数是另外一个binder(mAinfo)————IPersonManager,请忽略,理解内涵即可)。
最后会调取this.attchinfo(_arg0);
,也就是走到
private final IDogManager.DogManagerImpl mBinder = new IDogManager.DogManagerImpl() {
@Override
public List<Dog> getDogList() throws RemoteException {
Log.d("qqq","getDogList services");
return mDogsList;
}
@Override
public void attchinfo(IPersonManager iPersonManager) throws RemoteException {
Log.d("qqq","attchinfo services");
iPersonManager.addPersonDog(new Dog());
}
};
这里的attchinfo进行计算。
有返回结果的
case TRANSACTION_getDogList: {
data.enforceInterface(DESCRIPTOR);
java.util.List<com.example.binderservice.Dog> _result = this.getDogList();
reply.writeNoException();
reply.writeTypedList(_result);
return true;
}
reply.writeTypedList(_result);写结果进去(不同类别有差异,具体写法以aidl生成的文件为主)。
计算完成以后,会走return super.onTransact(code, data, reply, flags);
返回给Client端。
之前说过会在这里休眠
@Override public java.util.List<com.example.binderservice.Dog> getDogList() throws android.os.RemoteException { Log.d("qqq", "getDogList"); android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); java.util.List<com.example.binderservice.Dog> _result; try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(DogManagerImpl.TRANSACTION_getDogList, _data, _reply, 0); _reply.readException(); _result = _reply.createTypedArrayList(com.example.binderservice.Dog.CREATOR); } finally { _reply.recycle(); _data.recycle(); } return _result; }
当Service端计算出结果以后,会唤醒到这里。最后返回结果到调用处。
当然framework代码不完全是bind服务这种类型得到Ibinder对象,有些是直接传递的。但是大家可以这么理解
asBinder
是把自己实现的binder实体的引用发出去,这里对应service端的那个mBinder的引用。
asInterface
是Client去获得代理对象准备和Service通讯的。
举个原码例子。
在Instrumentation.java中
原码会用这个去startactivity,我们看看ATM.getService()
再看看asinterface
这里的asinterface就是去获得代理对象去和Service通讯。
所以接下来的ATM.getService().startActivity会掉到SystemServer进程中处理。
到此为止,一次binder通讯的调用就完成了。
新手第一次写博客,不对之处,请各位大佬指正。有疑问的话,欢迎交流
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。