近期忙着银联支付接口的对接,银联支付的接口采用的 AIDL 进行通讯。那么我就和大家一起分享 AIDL 是如何使用的?

AIDL 的全称为 Android Interface Definition Language ,接口描述语言。主要用于进程之间的通讯

新建service项目,新建AIDL文件

我这里使用的工具是 Android Studio ,新建 service 项目,我这里的包名是 com.github.service ,以 Packages 为目录结构,如下图新建 IRemoteService.aidl 文件:

// IRemoteService.aidl

package com.github.service;

// Declare any non-default types here with import statements

interface IRemoteService {

/**

* Demonstrates some basic types that you can use as parameters

* and return values in AIDL.

*/

void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,

double aDouble, String aString);

//aidl支持的参数类型有int long boolean float double String

}

AIDL支持下列所述的数据类型:

所有的基本类型(int、float等)

String

CharSequence

List

Map

实现Pacelable接口

接着在接口中添加方法:

interface IRemoteService {

/**

* Demonstrates some basic types that you can use as parameters

* and return values in AIDL.

*/

void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,

double aDouble, String aString);

//简单测试获取返回值

String baseAidl();

}

然后点击预编译按钮:

则 AIDL 文件生成对应的 Java 文件, 可以在 build/generated/source/aidl/debug 目录下找到这些 Java 文件。可以看到其内部有一个静态抽象类 Stub ,这个Stub 继承自 Binder 类,并抽象实现了其父接口,这里对应的是 IRemoteService这个接口:

public static abstract class Stub extends android.os.Binder implements com.github.service.IRemoteService

新建RemoteService服务

在 Java 目录下新建 RemoteService 类:

package com.github.service;

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.os.RemoteException;

import android.support.annotation.Nullable;

/**

* Created by Administrator on 9/19 0019.

*/

public class RemoteService extends Service {

private IRemoteService.Stub mIRemoteService = new IRemoteService.Stub() {

@Override

public String baseAidl() throws RemoteException {

return "你好啊,我是小智!";

}

@Override

public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

}

};

@Nullable

@Override

public IBinder onBind(Intent intent) {

return mIRemoteService.asBinder();

}

}

RemoteService 继承 Service ,在 onBind 方法中返回 IBinder 实例

一定不要忘记在 AndroidManifest.xml 清单文件中配置:

android:name=".RemoteService"

android:process=":remote">

绑定服务

Intent intent = new Intent(this, RemoteService.class);

bindService(intent, new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

IResultReceiver.Stub.asInterface(service);

Toast.makeText(MainActivity.this, "发送成功!", Toast.LENGTH_SHORT).show();

}

@Override

public void onServiceDisconnected(ComponentName name) {

}

}, BIND_AUTO_CREATE);

调用 bindService 方法进行服务的一个绑定

新建Client项目进行通讯

复制 Service 项目下的 aidl 目录到 Client 的 main 目录下:

当然你新建目录也是可以的,需要注意的是: aidl文件下的包名。

下面来看看 aidl 接口是怎么调用的:

package com.github.client;

import android.content.ComponentName;

import android.content.Intent;

import android.content.ServiceConnection;

import android.os.Bundle;

import android.os.IBinder;

import android.os.RemoteException;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import com.github.service.IRemoteService;

public class MainActivity extends AppCompatActivity {

private IRemoteService mIRemoteService;

private TextView tv;

private Button bt;

private static final String ACTION="com.github.service.RemoteService";

private static final String PACKAGE="com.github.service";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tv = (TextView) findViewById(R.id.tv);

bt = (Button) findViewById(R.id.bt);

bt.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

try {

if (mIRemoteService != null) {

String text = mIRemoteService.baseAidl();

if (text != null) {

tv.setText(text);

}

}else {

tv.setText("没有获取到服务器发送来的消息");

}

} catch (RemoteException e) {

e.printStackTrace();

}

}

});

}

@Override

protected void onResume() {

super.onResume();

Intent service = new Intent();

service.setAction(ACTION);

service.setPackage(PACKAGE);

bindService(service, new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

mIRemoteService = IRemoteService.Stub.asInterface(iBinder);

}

@Override

public void onServiceDisconnected(ComponentName componentName) {

}

}, BIND_AUTO_CREATE);

}

}

注意setAction是与service清单文件的action相匹配的,5.0以上的系统需要加上:service.setPackage(PACKAGE);

运行起来,我们一起来看看:

(责任编辑:最模板)

android 实现银联程序,Android银联支付之AIDL的基本使用相关推荐

  1. android系统应用程序,Android系统应用程序基本概念解读

    经常关注我们51CTO的朋友们应该知道,在以前的文章中我们对Android这一手机系统有一个详细的跟踪介绍,方便大家学习应用这一新的功能强大的开源手机系统,比如对Android源码的编译的解析的解析等 ...

  2. android小闹钟程序,Android实现闹钟小程序.pdf

    Android实实现现闹闹钟钟小小程程序序 这篇文章主要为大家详细介绍了Android实现闹钟小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 最近写了 闹钟的程序,看到SharedPrefe ...

  3. android espresso跨程序,Android Espresso:依次运行多个测试

    我正在尝试使用Espresso for Android进行一系列测试.运行之间似乎没有关闭活动.一次测试后,无论应用程序状态如何,左侧为下一次测试. 如何用Espresso来实现? 错误报告中提供的修 ...

  4. android 基础应用程序,android应用程序基本实现(基础篇).ppt

    <android应用程序基本实现(基础篇).ppt>由会员分享,可在线阅读,更多相关<android应用程序基本实现(基础篇).ppt(22页珍藏版)>请在人人文库网上搜索. ...

  5. android关键应用程序,Android应用程序基础.pdf

    创新自我 创造价值 创新办公室Innovation Office www innovation 创新办公室 Innovation Office www innovation 周行 admin zhou ...

  6. android调用微信程序,Android如何测试微信小游戏小程序?

    "微信小游戏性能评测标准建立的初衷是希望能引导开发者优化相关性能数据,提升用户体验.评测标准根据小游戏整体的性能数据表现.玩家体验评价,结合操作系统.机型分档.网络条件等多种维度建立.&qu ...

  7. android 进程 应用程序,Android中的每个活动都是一个进程,或者一个应用程序是一个进程...

    All activities inside an application run in one process? 这取决于应用程序清单中android:process属性的值. 如果没有为清单中的应用 ...

  8. android身份生成器程序,Android P 安全性更新

    原标题:Android P 安全性更新 Android P 引入了若干可提升应用和运行应用的设备安全性的功能. 本页面介绍对第三方应用开发者最重要的变化,需要他们牢记在心. 统一的指纹身份验证对话框 ...

  9. android查找邮件程序,Android 程序崩溃日志邮件获取

    版权声明:本文为博主原创文章,未经博主允许不得转载. 在我们开发Android应用程序的时候,BUG的出现是难以避免的,时不时还会出现崩溃的情况,这个时候,我们急需知道造成问题的原因是什么,但是,在没 ...

最新文章

  1. 离散正(余)弦信号的时域与FFT变换后所得频域之间的关系(幅值和相角)
  2. Intellij MyBatisPlus Plugin插件破解
  3. 岗位内推 | 字节跳动招聘NLP、计算机视觉、推荐算法实习生
  4. HTML DOM getElementsByName() 方法
  5. python运行不了指令_python不是内部命令或外部命令,也不是可执行程序解决方法...
  6. 从源码角度看Spark on yarn client cluster模式的本质区别
  7. 数值运算python嵩天_python语法_算数运算+赋值运算符+比较运算符+逻辑运算符
  8. linux 装完yum不能用,解决yum不能正常使用的问题
  9. mysql的collate_MYSQL中的COLLATE是什么?
  10. w3c subscribe
  11. 弹出对话框的同时保持页面的显示
  12. 使用Docker 安装jdk8
  13. quilt 工具增加 patch 方法
  14. Andorid 安卓接入支付宝支付(当面付)
  15. 获取微信公众号关注总人数和用户列表
  16. web_socket 协同文档编辑
  17. Python当中华氏度和摄氏度转换
  18. strncasecmp与strcasecmp用法
  19. npm ERR! code ENOENT npm ERR! syscall open npm ERR! path C:\Users\package.jsonnpm ERR! code ENOENT n
  20. 《Effective Java(第2版)》-Joshua Bloch等

热门文章

  1. JESD204B高速AD开发(二)LMK04821时钟芯片配置代码详解
  2. 实验3 绘制图像直方图、直方图像均衡化操作
  3. 2021年十一月八日到2021年十一月十四日笔记
  4. 以k8s集群管理为例,大牛教你如何设计优秀项目架构
  5. 正大国际期货:2022年各行业顶级富豪身价大洗牌
  6. 2021年终总结,幸运又令人感激的一年
  7. 数字化校园建设规划方案
  8. Windows 10剪贴板历史记录清理
  9. Java操作系统进程调度算法——时间片轮转(RR)算法
  10. 最新!考研统考阅卷结束!这些试卷易被压分!