Android学习(四)Binder Client

Binder Client

有了Binder DriverServiceManager以及Android系统专门面对应用开发提供的Binder封装,才能使应用程序之间顺利进行无缝通信。从四大组建中可以看出:

  • Activity:通过startActivity可以启动目标进程

  • Service:任何应用程序都可以通过startServicebindService来启动特定服务

  • Broadcast:任何应用程序都可以通过sendBroadcast来发送一个广播,且无论广播处理是否在同一进程中

  • Intent

bindService启动Service的步骤:

  1. 应用程序填写Intent,调用bindService发送请求

  2. 收到请求的bindService(此时还在应用程序的运行空间)将与ActivityManagerServiceAMS)取得联系。为了获得AMSBinder句柄,事先调用ServiceManager.getService,这里就涉及到进程间通信了。在得到AMS的句柄后,程序才能真正向它发送请求。

  3. AMS基于特定的最有匹配策略,从其内部存储的系统所有服务组件中找到与Intent最匹配的一个,然后向它发送Service绑定请求。如果目标进程还不存在的话,AMS还要负责把它启动。

  4. “被绑定”的服务进程需响应绑定,执行具体操作,并在成功后通知AMS。再由AMS回调发起请求的应用程序(回调接口是ServiceConnection)。

Context的相关继承关系:

150253_X0nh_117101.png

Activity继承关系的“根”是ContextbindService即包含在Context中,Context只是提供了抽象的接口:

/*\Android4.4\frameworks\base\core\java\android\content\Context.java*/

    public abstract boolean bindService(Intent service, ServiceConnection conn,

            int flags);

具体实现在ContextWrapper中:

/*\Android4.4\frameworks\base\core\java\android\content\Context.java*/

    @Override

    public boolean bindService(Intent service, ServiceConnection conn,

            int flags) {

        return mBase.bindService(service, conn, flags);

    }

mBase也是一个Context对象,ContexImpl实现(这里暂时留下疑问),bindService的实现如下:

/*\Android4.4\frameworks\base\core\java\android\app\ContextImpl.java*/

    @Override

    public boolean bindService(Intent service, ServiceConnection conn,

            int flags) {

        warnIfCallingFromSystemProcess();

        return bindServiceCommon(service, conn, flags, Process.myUserHandle());

    }

 

    /** @hide */

    @Override

    public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,

            UserHandle user) {

        return bindServiceCommon(service, conn, flags, user);

}

 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags,  UserHandle user) {

            int res = ActivityManagerNative.getDefault().bindService(

                mMainThread.getApplicationThread(), getActivityToken(),

                service, service.resolveTypeIfNeeded(getContentResolver()),

                sd, flags, user.getIdentifier());

}

 

ServiceManager一样,AMS也同样提供了ActivityManagerNativeActivityManagerProxy

/*\Android4.4\frameworks\base\core\java\android\app\ActivityManagerNative.java*/

    static public IActivityManager getDefault() {

        return gDefault.get();

}

    private static final Singleton<IActivityManager> gDefault = new Singleton<IActivityManager>() {à单实例,对象只被创建一次

        protected IActivityManager create() {

            IBinder b = ServiceManager.getService("activity");à通过ServiceManager取得AMSIBinder对象

            if (false) {

                Log.v("ActivityManager", "default service binder = " + b);

            }

            IActivityManager am = asInterface(b);à创建一个可用的ActivityManagerProxy

            if (false) {

                Log.v("ActivityManager", "default service = " + am);

            }

            return am;

        }

    };

ActivityManagerNative的作用之一,就是帮助调用者快速取得一个ActivityManagerProxy

 

ActivityManagerProxy中有bindService的实现:

/*\Android4.4\frameworks\base\core\java\android\app\ActivityManagerNative.java*/

public ActivityManagerProxy(IBinder remote)à构造函数中remote是上面通过ServiceManager.getService("activity");获取的

    {

        mRemote = remote;

    }  

 public int bindService(IApplicationThread caller, IBinder token,

            Intent service, String resolvedType, IServiceConnection connection,

            int flags, int userId) throws RemoteException {

        Parcel data = Parcel.obtain();

        Parcel reply = Parcel.obtain();

        data.writeInterfaceToken(IActivityManager.descriptor);

        data.writeStrongBinder(caller != null ? caller.asBinder() : null);

        data.writeStrongBinder(token);

        service.writeToParcel(data, 0);

        data.writeString(resolvedType);

        data.writeStrongBinder(connection.asBinder());

        data.writeInt(flags);

        data.writeInt(userId);

        mRemote.transact(BIND_SERVICE_TRANSACTION, data, reply, 0);

        reply.readException();

        int res = reply.readInt();

        data.recycle();

        reply.recycle();

        return res;

   

 

ActivityManagerNative的另一个作用是为ActivityManagerService的实现提供便利。可见在ActivityManagerNative中有如下代码:

/*\Android4.4\frameworks\base\core\java\android\app\ActivityManagerNative.java*/

    @Override

    public boolean onTransact(int code, Parcel data, Parcel reply, int flags)

            throws RemoteException {

        switch (code) {

        case START_ACTIVITY_TRANSACTION:

        {

            data.enforceInterface(IActivityManager.descriptor);

            IBinder b = data.readStrongBinder();

            IApplicationThread app = ApplicationThreadNative.asInterface(b);

            String callingPackage = data.readString();

            Intent intent = Intent.CREATOR.createFromParcel(data);

            String resolvedType = data.readString();

            IBinder resultTo = data.readStrongBinder();

            String resultWho = data.readString();

            int requestCode = data.readInt();

            int startFlags = data.readInt();

            String profileFile = data.readString();

            ParcelFileDescriptor profileFd = data.readInt() != 0

                    ? data.readFileDescriptor() : null;

            Bundle options = data.readInt() != 0

                    ? Bundle.CREATOR.createFromParcel(data) : null;

            int result = startActivity(app, callingPackage, intent, resolvedType,

                    resultTo, resultWho, requestCode, startFlags,

                    profileFile, profileFd, options);

            reply.writeNoException();

            reply.writeInt(result);

            return true;

        }

}

ActivityManagerService继承自ActivityManagerNative,所以ActivityManagerNative既是面向调用者,也是面向服务本身的。

ActivityManagerService的内容异常繁杂,内容还有很多,暂时先学习到这里,与ServiceManager发生了联系。

 

转载于:https://my.oschina.net/honeyandroid/blog/505880

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值