Android Binder机制原理java层系列一(有图有代码很详细)

binder相关学习都是参考老罗和邓凡平的相关文章学习总结的,
自己在学习的过程中,发现画图总结比较容易记忆,这里分享出来!!
当然如果你看过老罗或者邓凡平的博客,再来看看该篇博客也许效果会更好!!

关于Binder的java层机制讲解,大概会分为三篇博客讲解
Android Binder机制原理java层系列一(有图有代码很详细)
Android Binder机制原理java层系列二(有图有代码很详细)
Android Binder机制原理java层系列三(有图有代码很详细)

因为下面说到了aidl,这里给出aidl的类结构
(这里大家可以不用细看,直接看下面的画图即可,图中说到了aidl可以再回头看看)

interface IDonService {
   void addService(ServiceBean serviceBean);
   ServiceBean getService();
   int myPid();
}

看下编译后的类结构

public interface IDonService extends android.os.IInterface {
    /**
     * Local-side IPC implementation stub class.
     */
    public static abstract class Stub extends android.os.Binder implements com.rain.meter.main.lib.IDonService {
        private static final java.lang.String DESCRIPTOR = "com.rain.meter.main.lib.IDonService";
        /**
         * Construct the stub at attach it to the interface.
         */
        public Stub() {
            this.attachInterface(this, DESCRIPTOR);
        }
        /**
         * Cast an IBinder object into an com.rain.meter.main.lib.IDonService interface,
         * generating a proxy if needed.
         */
        public static com.rain.meter.main.lib.IDonService asInterface(android.os.IBinder obj) {
            if ((obj == null)) {
                return null;
            }
            android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
            if (((iin != null) && (iin instanceof com.rain.meter.main.lib.IDonService))) {
                return ((com.rain.meter.main.lib.IDonService) iin);
            }
            return new com.rain.meter.main.lib.IDonService.Stub.Proxy(obj);
        }
        @Override
        public android.os.IBinder asBinder() {
            return this;
        }
        @Override
        public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {
            switch (code) {
                case INTERFACE_TRANSACTION: {
                    reply.writeString(DESCRIPTOR);
                    return true;
                }
                case TRANSACTION_addService: {
                    data.enforceInterface(DESCRIPTOR);
                    com.rain.meter.main.lib.bean.ServiceBean _arg0;
                    if ((0 != data.readInt())) {
                        _arg0 = com.rain.meter.main.lib.bean.ServiceBean.CREATOR.createFromParcel(data);
                    } else {
                        _arg0 = null;
                    }
                    this.addService(_arg0);
                    reply.writeNoException();
                    return true;
                }
                case TRANSACTION_getService: {
                    data.enforceInterface(DESCRIPTOR);
                    com.rain.meter.main.lib.bean.ServiceBean _result = this.getService();
                    reply.writeNoException();
                    if ((_result != null)) {
                        reply.writeInt(1);
                        _result.writeToParcel(reply, android.os.Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
                    } else {
                        reply.writeInt(0);
                    }
                    return true;
                }
                case TRANSACTION_myPid: {
                    data.enforceInterface(DESCRIPTOR);
                    int _result = this.myPid();
                    reply.writeNoException();
                    reply.writeInt(_result);
                    return true;
                }
            }
            return super.onTransact(code, data, reply, flags);
        }
        private static class Proxy implements com.rain.meter.main.lib.IDonService {
            private android.os.IBinder mRemote;
            Proxy(android.os.IBinder remote) {
                mRemote = remote;
            }
            @Override
            public android.os.IBinder asBinder() {
                return mRemote;
            }
            public java.lang.String getInterfaceDescriptor() {
                return DESCRIPTOR;
            }
            @Override
            public void addService(com.rain.meter.main.lib.bean.ServiceBean serviceBean) throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    if ((serviceBean != null)) {
                        _data.writeInt(1);
                        serviceBean.writeToParcel(_data, 0);
                    } else {
                        _data.writeInt(0);
                    }
                    mRemote.transact(Stub.TRANSACTION_addService, _data, _reply, 0);
                    _reply.readException();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
            @Override
            public com.rain.meter.main.lib.bean.ServiceBean getService() throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                com.rain.meter.main.lib.bean.ServiceBean _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    mRemote.transact(Stub.TRANSACTION_getService, _data, _reply, 0);
                    _reply.readException();
                    if ((0 != _reply.readInt())) {
                        _result = com.rain.meter.main.lib.bean.ServiceBean.CREATOR.createFromParcel(_reply);
                    } else {
                        _result = null;
                    }
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }
            @Override
            public int myPid() throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                int _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    mRemote.transact(Stub.TRANSACTION_myPid, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readInt();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }
        }
        static final int TRANSACTION_addService = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
        static final int TRANSACTION_getService = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
        static final int TRANSACTION_myPid = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
    }
    public void addService(com.rain.meter.main.lib.bean.ServiceBean serviceBean) throws android.os.RemoteException;
    public com.rain.meter.main.lib.bean.ServiceBean getService() throws android.os.RemoteException;
    public int myPid() throws android.os.RemoteException;
}

实现IDonservice.Stub

public class DonService extends Service {
    private ServiceBean mServiceBean;
    private IBinder mIBinder = new IDonService.Stub() {
        @Override
        public void addService(ServiceBean serviceBean) throws RemoteException {
            mServiceBean = serviceBean;
        }
        @Override
        public ServiceBean getService() throws RemoteException {
            return mServiceBean;
        }
        @Override
        public int myPid() {
            return Process.myPid();
        }
    };
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mIBinder;
    }
}

具体看下在哪里使用

public class Donmain {
    private IDonService iDonService;
    private volatile static Donmain instance;
    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //连接后拿到 Binder,转换成 AIDL,在不同进程会返回个代理
            iDonService = IDonService.Stub.asInterface(service);
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            iDonService = null;
        }
    };
    private Donmain(){}
    public static Donmain getInstance(){
        if (instance == null){
            synchronized (Donmain.class){
                if (instance == null){
                    instance = new Donmain();
                }
            }
        }
        return instance;
    }
    public void getService(Context context){
        Intent intent1 = new Intent(context.getApplicationContext(), DonService.class);
        context.bindService(intent1, mConnection, Context.BIND_AUTO_CREATE);
        if (iDonService != null){
            try {
                int pid = iDonService.myPid();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }
}

Binder机制在java层,主要通过ServiceManager,Service和Client

1.ServiceManager主要把各种系统服务add到系统中管理
ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);
2.Service就是我们提供远程接口服务一端,即实现了Binder接口的类
列举几个:
1.我们经常写的aidl
public static abstract class Stub extends android.os.Binder implements com.rain.meter.main.lib.IDonService
2.ServiceManager的真正管理者,ServiceManagerNative
/**
 * Native implementation of the service manager.  Most clients will only
 * care about getDefault() and possibly asInterface().
 * @hide
 */
public abstract class ServiceManagerNative extends Binder implements IServiceManager

看到没,他们都继承了Binder接口,即有了跨进程通信的能力,
但是这里注意下,他们都是abstract,那么具体实现方法肯定在实现类中完成

1.aidl的实现类很简单,就是我们经常写的这种格式,
即IBinder mIBinder = new IDonService.Stub
public class DonService extends Service {
    private ServiceBean mServiceBean;
    private IBinder mIBinder = new IDonService.Stub() {
        @Override
        public void addService(ServiceBean serviceBean) throws RemoteException {
            mServiceBean = serviceBean;
        }
        @Override
        public ServiceBean getService() throws RemoteException {
            return mServiceBean;
        }
        @Override
        public int myPid() {
            return Process.myPid();
        }
    };
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
   Thread.currentThread().getName());
        return mIBinder;
    }
}

2.ServiceManagerNative的实现,看这个类的注释就会发现,
类很特别,它是在IServiceManager.cpp文件中实现的,看下部分代码

namespace android {
sp<IServiceManager> defaultServiceManager()
{
    if (gDefaultServiceManager != NULL) return gDefaultServiceManager;

    {
        AutoMutex _l(gDefaultServiceManagerLock);
        while (gDefaultServiceManager == NULL) {
            gDefaultServiceManager = interface_cast<IServiceManager>(
                ProcessState::self()->getContextObject(NULL));
            if (gDefaultServiceManager == NULL)
                sleep(1);
        }
    }

    return gDefaultServiceManager;
}

好了以上就是提供服务的Service端内容,他们肯定都是继承或者实现了IBinder(Binder实现了IBinder),即有了跨进程通信的能力后,才能提供服务

3.Client端,就是获取服务的一端,这个很容易理解
1.对于aidl即我们获取aidl服务的地方
 context.bindService(intent1, mConnection, Context.BIND_AUTO_CREATE);
 然后在mConnection的onServiceConnected中:IDonService.Stub.asInterface(service);
 然后使用远程服务iDonService的各种方法
2.ServiceManager即我们get或者add服务的地方
 getSystemService(Context.ACTIVITY_SERVICE);
 ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);

ServiceManager,Service和Client之间到底是如何通信的

Client端要使用Service提供的远程服务,首先必须从ServiceManager那里获取Service的服务,

IDonService.Stub.asInterface(service);
getSystemService(Context.ACTIVITY_SERVICE);

1.但是呢,这里获取到的不是真正的Service,而是它的代理Proxy,
2.这个代理并没有继承或者实现Binder相关类或者接口,它本身不具备跨进程通信能力,
3.但是代理里面有个mRemote,这才是一个实现了IBinder的类,即BinderProxy(下面会详细说到BinderProxy都是啥)
4.当我们获取Service或者使用Service提供的方法时,就和Binder底层驱动交互了
5.底层Binder驱动又和ServiceManager交互
6.ServiceManager通过驱动又和Service交互

这里大家一定要知道,对于整个通信过程

Client要通过BinderProxy-->它的Native层映射就是BpBinder
Service要通过Binder-->它的Native层映射就是JavaBBinder

上个图感受下!
image.png

注意,图片有点大,要点击原图查看

下面先说点基础知识


1.png
上图中gBinderOffsets和gBinderProxyOffsets顾名思义了
gBinderInternalOffsets以及它对应的java层BinderInternal类,它们都是Binder层用的类,我们一般开发程序不会使用,它的内部有个GcWatcher,该类用来处理和Binder架构有关的垃圾回收

下一篇我会详细讲解getService和addService,从里面引出上面各个对象之间的关系,相信你看完该篇博客会对Binder(java层)有个详细了解
Android Binder机制原理java层系列二(有图有代码很详细)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,036评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,046评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,411评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,622评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,661评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,521评论 1 304
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,288评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,200评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,644评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,837评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,953评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,673评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,281评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,889评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,011评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,119评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,901评论 2 355