首页 技术 正文
技术 2022年11月9日
0 收藏 974 点赞 2,823 浏览 3104 个字

前言:

前面总结了程序间共享数据,可以使用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一致
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295