源码分析原理解析Android开发

bindService之AIDL使用和调用流程分析

2017-06-08  本文已影响80人  umbrella1

看下总体流程图:



ServerApplication工程:
1.服务端ServerApplication 创建AIDL文件:
在包名cn.umbrella.aidl下右键-->new-->AIDL-->AIDL File,创建aidl文件IUmBrellaService,android studio会把aidl文件统一自动放到main/aidl目录下
IUmBrellaService.aidl:

// IUmBrellaService.aidl
package cn.umbrella.aidl;
// Declare any non-default types here with import statements
interface IUmBrellaService {
    String umbrellaEevent(String client_str);
}

重新rebuild工程下,这个aidl文件就会自动生成IUmBrellaService.java文件,在build/generated/source/aidl/debug/目录下:

public interface IUmBrellaService extends android.os.IInterface {
    public static abstract class Stub extends android.os.Binder implements IUmBrellaService {
        private static final String DESCRIPTOR = "cn.umbrella.aidl.IUmBrellaService";
        public Stub() {
            this.attachInterface(this, DESCRIPTOR);
        }
        public static IUmBrellaService asInterface(android.os.IBinder obj) {
            ...
            return new Stub.Proxy(obj);
        }
        @Override
        public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {
            switch (code) {
                case TRANSACTION_umbrellaEevent: {
                    data.enforceInterface(DESCRIPTOR);
                    String _arg0;
                    _arg0 = data.readString();
                    String _result = this.umbrellaEevent(_arg0);
                    reply.writeNoException();
                    reply.writeString(_result);
                    return true;
                }
            }
            return super.onTransact(code, data, reply, flags);
        }
        private static class Proxy implements IUmBrellaService {
            private android.os.IBinder mRemote;
            Proxy(android.os.IBinder remote) {
                mRemote = remote;
            }
            @Override
            public String umbrellaEevent(String client_str) throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                String _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    _data.writeString(client_str);
                    mRemote.transact(Stub.TRANSACTION_umbrellaEevent, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readString();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }
        }
        static final int TRANSACTION_umbrellaEevent = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
    }
    public String umbrellaEevent(String client_str) throws android.os.RemoteException;
}

这个java包括了静态抽象Stub类,以及私有Proxy,这是客户端client和服务端Service 通过binder驱动通讯用。
2、提供外部绑定的service以及在XML上声明权限:
ServerService.java:

public class ServerService extends Service {
    private IBinder mBinder;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        mBinder = new IUmBrellaImpl();
        return mBinder;
    }
    public class IUmBrellaImpl extends IUmBrellaService.Stub{
        @Override
        public String umbrellaEevent(String client_str) throws RemoteException {
            Log.d("ServerService","client_str=" + client_str);
            String serverString = "server string is from server";
            return serverString;
        }
    }
}

其中onBind返回的对象IBinder给客户端client 绑定服务成功回调函数onServiceConnected。
XML声明permission和action:

<service
     android:name=".service.ServerService"
     android:exported="true">
     <intent-filter>
           <action android:name="android.intent.action.ServerService"/>
      </intent-filter>
</service>    

这样服务端server就完毕,接下来我们看客户端client如何绑定:
3、客户端client同样需要创建一样的aidl文件,并且路径都一致,重新rebuild下,也会自动在build/generated/source/aidl/debug/生成IUmBrellaService.java文件,内容都一致。
绑定bindservice:

private Context mContext;
private IBinder mBinder;
IUmBrellaService mServerSerivce;
private void connect(){
        Intent intent = new Intent("android.intent.action.ServerService");
        ComponentName cpName = new ComponentName("cn.umbrella.server","cn.umbrella.server.service.ServerService");
        intent.setComponent(cpName);
        mContext.bindService(intent,mServiceConnection,Service.BIND_AUTO_CREATE);
    }
    ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            try {
                mBinder = service;
                mServerSerivce = IUmBrellaService.Stub.asInterface(binder);
                mBinder.linkToDeath(mDeatRecipient,0);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
    IBinder.DeathRecipient mDeatRecipient = new IBinder.DeathRecipient() {
        @Override
        public void binderDied() {
            Log.d("","远程进程died");
        }
    };

其中onServiceConnected(ComponentName name, IBinder binder) 的参数binder就是客户端绑定服务端ServerService 成功后onBinder返回的对象,在这边把binder改造成IUmBrellaService.Stub的Proxy类,即 mServerSerivce = IUmBrellaService.Stub.asInterface(binder);
这样绑定成功后,客户端client就可以调用服务端server提供的接口即:

try {
     String is  =  mServerSerivce.umbrellaEevent("umbrella event is from client");
    } catch (RemoteException e) {
         e.printStackTrace();
 }                                      

对比startservice,使用bindservice相对来说中度耦合,可短时间内密集通讯,识别呼叫端、可回传实行结果及listener回调、service进程crash,呼叫端可以收到died,容错处理,bindservice成功后,调用接口就不会再通过ActivityManagerService了。

上一篇下一篇

猜你喜欢

热点阅读