• Android探索之AIDL实现进程间通信


    前言:

         前面总结了程序间共享数据,可以使用ContentProvider也可以使用SharedPreference,那么进程间怎么共享内存呢?Android系统中的进程之间不能共享内存,因此,需要提供一些机制在不同进程之间进行数据通信。

    为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用(Remote Procedure Call,RPC)方式来实现。与很多其他的基于RPC的解决方案一样,Android使用一种接口定义语言(Interface Definition Language,IDL)来公开服务的接口。我们知道4个Android应用程序组件中的3个(Activity、BroadcastReceiver和ContentProvider)都可以进行跨进程访问,另外一个Android应用程序组件Service同样可以。因此,可以将这种可以跨进程访问的服务称为AIDL(Android Interface Definition Language)服务。

    接下来实战一下具体实现:

     1.)首先新建一个aidl文件
    interface ITestInterface {
         //获取进程ID
        int getProcessId();
    
        //处理字符串
        String dealString( String srcString);
    
        //字符串追加
        String appendString( String srcString);
    
        void addPerson(in Person person);
    
        List<Person> getPersons();
    }

    aidl语法解说:

    •   声明函数基本和Java一致,可以传参和返回值,参数和返回值
    •   参数和返回值   Java编程语言的基本数据类型 (int, long, char, boolean等),String和CharSequence,集合接口类型List和Map、其他AIDL接口类型、实现Parcelable接口的自定义对象
    •   方向指示    在使用aidl传输数据时,对于非基本数据类型,也不是String和CharSequence类型的,(即Parcelable类型)需要有方向指示,包括in、out和inout。
    •   AIDL只支持接口方法,不能公开static变量。

    2.)服务端实现接口

        private final ITestInterface.Stub mBinder = new ITestInterface.Stub() {
    
            public int getProcessId(){
                Log.e("TestService","TestService  Thread: " + Thread.currentThread().getName());
                Log.e("TestService","TestService  getProcessId()");
                return  android.os.Process.myPid();
            }
            //处理字符串
            public String dealString( String srcString)
            {
                return srcString+srcString;
            }
    
            //字符串追加
            public  String appendString( String srcString)
            {
                return srcString+srcString;
            }

    3.)客户端获取接口

       private  ServiceConnection connection = new ServiceConnection() {
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.e("TestService","TestService  onServiceDisconnected()");
            }
    
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                iTestInterface =    ITestInterface.Stub.asInterface(service);
                try {
                    Log.e("TestService","TestService  onServiceConnected()");
                    int remoteId=iTestInterface.getProcessId();
                    Log.e("TestService","TestService  remoteId---->"+remoteId);
                    int currentPid = android.os.Process.myPid();
                    Log.e("TestService","TestService  currentPid---->"+currentPid);
                    Log.e("TestService","TestService  dealString---->"+iTestInterface.dealString("Remote Service"));
                    Log.e("TestService","TestService  appendString---->"+iTestInterface.appendString("Remote Service"));
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        };

    4.)通过IPC调用/传递数据

                    int remoteId=iTestInterface.getProcessId();
                    Log.e("TestService","TestService  remoteId---->"+remoteId);
                    int currentPid = android.os.Process.myPid();
                    Log.e("TestService","TestService  currentPid---->"+currentPid);
                    Log.e("TestService","TestService  dealString---->"+iTestInterface.dealString("Remote Service"));
                    Log.e("TestService","TestService  appendString---->"+iTestInterface.appendString("Remote Service"));

    5.)Service声明以及绑定/解绑 

    声明:

           <service
                android:name=".TestService"
                android:enabled="true"
                android:exported="true"
                android:label="remoteService"
                android:process=":remote">
                <intent-filter android:priority="1000">
                    <category android:name="android.intent.category.DEFAULT" />
                    <action android:name="com.whoislcj.testaidl.TestService" />
                </intent-filter>
            </service>

    绑定:

                    Intent intent = new Intent("com.whoislcj.testaidl.TestService");
                    intent.setPackage(getPackageName());//这里你需要设置你应用的包名
                    bindService(intent, connection, Context.BIND_AUTO_CREATE);

    解绑:

                   unbindService(connection);
    6.)访问权限同Service一致
  • 相关阅读:
    RTSP协议转RTMP协议的行业视频接入网关EasyRTSPLive如何实现音频转码的
    RTSP协议转RTMP协议的行业视频接入网关EasyRTSPLive之跨平台ini配置及通道的方法
    GB/T28181协议EasyGBS播放1080p视频直播会花屏
    国标GB/T28181协议下播放器起播慢或者延迟高如何解决?
    EasyGBS查找大华设备的录像列表时失败
    ffmpeg增加h264编解码功能模块方法
    EasyNVR控制台运行出现invalid license关于计算机保护软件类似于360、腾讯云管家等限制相关问题
    摄像机经过多级路由转换无法被EasyNVR拉流问题处理方法
    使用EasyNVR软件对接海康摄像头对接失败问题解析
    GB/T28181协议使用EasyNVR降低播放延迟方法
  • 原文地址:https://www.cnblogs.com/whoislcj/p/5509868.html
Copyright © 2020-2023  润新知