探究android-aidl---bindService的绑定及其内部原理

本文详细剖析了Android中bindService的使用、工作流程及内部实现,包括ServiceConnection的角色、AMS调度和服务端的处理过程。通过对源码的解读,阐述了客户端和服务端如何通过IServiceConnection进行跨进程通信,以及服务启动和绑定的整个生命周期。
摘要由CSDN通过智能技术生成

bindService的用法

    private IMyAidlInterface iMyAidlInterface;

    private void bindService() {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.example.aidlserver", "MyService"));
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
            try {
                iMyAidlInterface.hi("");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

bindService的大致流程

bindService涉及哪些参与者
1.客户端进程(请求端) 2.系统进程   3.serviceManager进程  4. 服务端进程
通讯过程是怎样的?

1. bindService时传递的ServiceConnection参数
在绑定服务时调用bindService(Intent service, ServiceConnection conn, int flags),在绑定成功后的ServiceConnection对象里onServiceConnected()将会收到服务端传来的代理对象。所以先从bindService时传入的ServiceConnection对象开始探究,这个ServiceConnection对象如何被保存?如何通过ServiceConnection对象回调服务代理对象?
根据bindService()源码看下:
frameworks\base\core\java\android\app\ContextImpl.java

@Override
    public boolean bindService(Intent service, ServiceConnection conn,
            int flags) {
        warnIfCallingFromSystemProcess();
        return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), getUser());
    }

接着看下bindServiceCommon():
frameworks\base\core\java\android\app\ContextImpl.java

private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
            handler, UserHandle user) {
        // Keep this in sync with DevicePolicyManager.bindDeviceAdminServiceAsUser.
        IServiceConnection sd;
        ...
        if (mPackageInfo != null) {
			//根据请求时传入的ServiceConnection获取IServiceConnection对象
            sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
        } 
        ...
			// IServiceConnection对象传入到ams中,进行bindService
         	int res = ActivityManager.getService().bindService(mMainThread.getApplicationThread(), getActivityToken(), service,
                		service.resolveTypeIfNeeded(getContentResolver()),  sd, flags, getOpPackageName(), user.getIdentifier());
          ...
    }

接下来再看下 mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
这里怎么根据请求时传入的ServiceConnection获取IServiceConnection对象,这个IServiceConnection对象有什么用处?
frameworks\base\core\java\android\app\LoadedApk.java

    public final IServiceConnection getServiceDispatcher(ServiceConnection c,
            Context context, Handler handler, int flags) {
        synchronized (mServices) {
            LoadedApk.ServiceDispatcher sd = null;
			// ArrayMap<Context, ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher>> mServices
       		// mServices中存着当前进程所有请求绑定服务的context和对应的接收服务的派遣对象
            ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher> map = mServices.get(context);
			//如果当前上下文从未绑定过服务则map为空
            if (map != null) {
                if (DEBUG) Slog.d(TAG, "Returning existing dispatcher " + sd + " for conn " + c);
				// 根据ServiceConnection获取ServiceDispatcher对象;
				// ServiceDispatcher用于系统进程返回服务代理给客户端时的派遣
                sd = map.get(c);
            }
			//如果当前ServiceConnection首次传入绑定服务时,map里没有该sd
            if (sd == null) {
				//新建ServiceDispatcher对象
                sd = new ServiceDispatcher(c, context, handler, flags);
                if (DEBUG) Slog.d(TAG, "Creating new dispatcher " + sd &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值