zoukankan      html  css  js  c++  java
  • 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一致
  • 相关阅读:
    Oracle数据表解锁
    VS2008 新建网站时没有模板解决办法
    64位WIN2008中组件服务中的DCOM配置找不到Microsoft Excel应用程序的解决办法
    Log4Net Appender配置
    使用过滤器过滤asp.net mvc输出内容
    Html.DropDownList也能绑定数据
    iirf有时有效,有时返回404错误的解决方法
    Oracle优化AutoTrace输出内容的含义
    Oracle使用Sql把XML解析成表(Table)的方法
    文件跨系统ftp传输后无法还原了,哎
  • 原文地址:https://www.cnblogs.com/whoislcj/p/5509868.html
Copyright © 2011-2022 走看看