Android IPC通信和AIDL技术应用
2023-09-14 09:08:08 时间
首先我们了解一下 IPC和AIDL
IPC:进程间通信
AIDL:Android Interface Definition Language,即Android接口定义语言。 为什么使用: 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)服务。
说了那么久还是有些抽象,那么就举一个小样例。可能不太合适,可是仅作为帮助理解使用。
既如今有一个用户要在京东商城购买东西,可是在付款的时候须要调用支付插件(就支付宝ba ),支付宝肯定不会把核心付款的代码给你,于是支付宝仅仅能给你一个方法
int pay(int money);
这里面有须要传递的參数(价格),返回是否付款成功的返回值 ,让用户在京东商城就可以付款。
如今开发这个需求 。首先定义一个接口供京东商城调用,此文件名称为PayInterface.aidl
package com.example.alipay; interface PayInterface { int pay(int money); }然后我定义自己的实现方法。开启服务。接收京东商城用户调用。
package com.example.alipay; import com.example.alipay.PayInterface.Stub; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.os.RemoteException; public class AliPayService extends Service{ @Override public void onCreate() { System.out.println("开启远程支付服务"); super.onCreate(); } @Override public IBinder onBind(Intent intent) { return new MyIBinder(); } @Override public boolean onUnbind(Intent intent) { return super.onUnbind(intent); } @Override public void onDestroy() { super.onDestroy(); } private class MyIBinder extends Stub{ @Override public int pay(int money) throws RemoteException { if(money == 200) return 1; return 0; } } }MyIBinder extends Stub这个类。用户在商城付款时绑定服务返回的对象,stub是PayInterface.aidl文件定义完毕后后自己主动生成的JAVA类,在该类中实现了自己的支付核心代码,然后再主程序安装此app;相当于安装了支付插件,安装完毕后如图所看到的;
接着,我们须要开发商城app,调用远程支付服务。首先将PayInterface.aidl(相当于支付宝给你的开发API。)拷贝到Androidproject下。
这里应注意包名和aidl中定义包名全然一致,复制完毕后自己主动生成
PayInterface.java文件,然后我们在应用中调用远程支付功能。
package com.example.consumer; import com.example.alipay.PayInterface; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.view.Menu; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { private PayInterface pi; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent("com.taobao.alipay"); ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { pi = PayInterface.Stub.asInterface(service); } }; bindService(intent, conn , BIND_ADJUST_WITH_ACTIVITY); } public void pay(View v) throws RemoteException{ int result = pi.pay(200); if(result == 1){ Toast.makeText(getApplicationContext(), "支付成功", 0).show(); } else { Toast.makeText(getApplicationContext(), "支付失败", 0).show(); } } }将应用程序公布到模拟器上执行,点击支付button,出现例如以下界面;
调用成功,这大致算是一个远程服务的样例吧,希望能让大家更清晰的明确aidl的引用。
源代码下载 点击打开链接
版权声明:本文博客原创文章。博客,未经同意,不得转载。
相关文章
- 聚焦 Android 11: 大功告成
- android图片资源加密,Android平台图像文件加密
- GB28181状态信息报送解读及Android端国标设备接入技术实现
- android sdk安装过程,图文详解Android 3.0 SDK安装教程
- Android面试准备之集合
- 【Android 安装包优化】Android 中使用 SVG 图片 ( 批量转换 SVG 格式图片为 Vector Asset 矢量图资源 )
- 【Android 插件化】多开原理 | 使用插件化技术的恶意应用 | 插件化的其它风险 | 应用开发推荐方案
- 【Android 逆向】修改运行中的 Android 进程的内存数据 ( 使用 IDA 分析要修改的内存特征 | 根据内存特征搜索修改点 | 修改进程内存 )
- 【错误记录】未安装该应用 ( 在 Android 12 之后 组件设置 android:exported=“false“ 属性 )
- 【Android Gradle 插件】BuildType 编译类型配置 ⑤ ( renderscriptDebuggable 配置 | shrinkResources 配置 )
- 【错误记录】Android Studio 集成 ARoute 编译报错 ( 兼容 support 库和 androidx 库 | add ‘tools:replace=“android:appCo )
- 【Android 应用开发】Android 无障碍开发简介 ( Android 无障碍开发辅助技术 | 启用 TalkBack 无障碍功能 | TalkBack 无障碍开发 示例 )
- [android] 手机卫士读取联系人详解手机开发
- [android] 相对布局和单位简介详解手机开发
- android之动画popowindows详解手机开发
- Android LruCache技术原理详解手机开发
- Android 写一个属于自己的Rxjava(二)详解手机开发
- Android 屏幕刷新机制:ViewRootImpl、Choreographer、Surface、SurfaceFlinger关系详解手机开发
- android开发中常用的Eclipse快捷键详细整理
- Android异步获取网络图片并处理导致内存溢出问题解决方法
- Windows下获取Android源码方法的详解
- Android轻松实现图片倒影效果实例代码