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();
}
}
}
}
}
1 大家都知道binder的第一步是services端向serverManager注冊,
那么代碼的第一步在哪里呢?如何向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);),
2 第二步,Client端向ServerManager端查詢binder服務,拿到代理物件,
關鍵代碼是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);
第三步,使用代理物件的方法,請Service端計算
關鍵方法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就回傳到你呼叫的地方,
第四步,binder經過一系列系統呼叫,到servceis端計算并回傳結果
關鍵方法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端,
第五步,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通訊的呼叫就完成了,
新手第一次寫博客,不對之處,請各位大佬指正,有疑問的話,歡迎交流
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/240556.html
標籤:其他
