栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android AIDL实现进程间通信探索

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 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声明以及绑定/解绑  

声明: 

    
      
 
 
      
     

绑定:               

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

解绑:unbindService(connection);

6.)访问权限同Service一致

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/161278.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号