Android多进程间采用AIDL方式进行通信

  

在上一节中,我介绍了Android中服务的生命周期以及一些有关知识。在这一节中,我采用代码编写的方式来介绍一下不同程序之间也就是不同进程之间通信采用AIDL方式。

  

首先我需要解释一下,不同程序进程间采用AIDL方式启动服务,我们可以看作成客户端客户端与服务器服务端之间的通信,无非c/s都是安装在了我们的智能手机设备Android系统之上。好了,理解到这里我们就可以继续往下介绍了。

  

首先我们编写服务器服务端程序:

  

1)我们新建一个应用程序,它的应用程序架构如下:

  

 Android多进程间采用AIDL方式进行通信

  

2)我们在com.lgy.s包下编写S.aidl文件,具体代码如下:(aidl编码格式不再叙述)

        包com.lgy.s;      接口年代{   getStr字符串(字符串名称);   }   之前      

编写好S.aidl文件我们就可以使用。存根类下的相关方法。
  

  

3)我们可以自定义我们的服务了,具体代码如下:

        包com.lgy.s;      进口android.app.Service;   进口android.content.Intent;   进口android.os.IBinder;   进口android.os.RemoteException;   进口android.util.Log;      公开课MyService延伸服务{      私有静态最终字符串标签=癕yService”;   私人的年代。服务器存根;      @Override   公共空间>   & lt;服务android: name=癱om.lgy.s.MyService”比;   & lt;意图过滤器比;   & lt;行动android: name=" android.lgy。myService”/比;   & lt;/intent-filter>   & lt;/service>   之前      

- - - - - - - - - - - -到此我们服务器端就编写完毕- - - - - - - - - - - - - - - - - - - - - - - -
  

  

下面我们编写客户端端应用程序:

  

1)我们新建一个应用程序C,具体应用架构如下:

  

 Android多进程间采用AIDL方式进行通信

  

2)我们将在服务器端年代写的aidl原封不动的移到客户端C上来(注包文件名都原封不动),移动后架构如下图:

  

 Android多进程间采用AIDL方式进行通信

  

3)我们就可以在客户端MainActivity中直接调用绑定服务器上的服务,具体代码如下:

        包com.lgy.c;      进口android.app.Activity;   进口android.content.ComponentName;   进口android.content.Intent;   进口android.content.ServiceConnection;   进口android.os.Bundle;   进口android.os.IBinder;   进口android.os.RemoteException;   进口android.util.Log;   进口android.view.View;      进口com.lgy.s.S;      公开课MainActivity延伸活动{      保护静态最终字符串标签=癕ainActivity”;   私人年代年代;   私人ServiceConnection康涅狄格州=new ServiceConnection () {   @Override   公共空间>   & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android "   xmlns:工具=" http://schemas.android.com/tools "   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android: paddingBottom=" @dimen/activity_vertical_margin”   android: paddingLeft=" @dimen/activity_horizontal_margin”   android: paddingRight=" @dimen/activity_horizontal_margin”   android: paddingTop=" @dimen/activity_vertical_margin”   工具:上下文=" com.lgy.c。祝辞MainActivity”;      & lt;按钮   android: id=癅 + id/button1”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android: layout_alignParentLeft=" true "   android: layout_alignParentTop=" true "   android: onClick=" bindBtn "   android:文本="绑定服务器服务”/比;      & lt;按钮   android: id=癅 + id/button2”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android: layout_alignLeft=癅 + id/button1”   android: layout_below=癅 + id/button1”   android: onClick=" greetBtn "   android:文本="传递参数给服务器获取返回的数据”/比;      & lt;按钮   android: id=癅 + id/button3”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android: layout_alignLeft=癅 + id/button2”   android: layout_below=癅 + id/button2”   android: onClick=" unbindBtn "   android:文本="解除绑定服务”/比;      & lt;/RelativeLayout>   

Android多进程间采用AIDL方式进行通信