android studio 使用 aidl(一)基础用法
最近公司需要开发一个项目用的到aidl,之前研究过eclipse版本的,但是好久了一直没用,现在需要捡起来,但是现在都用android studio了,所以查了下资料 都不是很全,我在这里总结一下,方便后续忘了在用到。
第一步:通过as创建一个aidl文件,在app右键,如下图:

输入自己想要的名字,别的都默认,点击Finish 我这里的名字叫 PayAidlInterface 创建好如下:

在看看 PayAidlInterface.aidl 里面怎么写的,其实就一个计算的方法 客户端传2个int类型的值,服务端计算和
// PayAidlInterface.aidl
package com.txy.umpay.aidl; // Declare any non-default types here with import statements interface PayAidlInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
int calculation(int anInt, int bnInt);
}
第二步: PayAidlInterface.aidl 编写完成之后 需要Build-->Make Module app,生成相应的java文件,如下图:

在来看看生成的java文件的位置:

第三步:接下来,就该完成我们的MAIDLService逻辑部分了,MAIDLService.java代码如下:
先说下我遇到的坑,我是通过as右键创建的service 他自动会加上下面2个属性 就会导致客户端调用不起来,所以记得一定要删除
android:enabled="false"
android:exported="false"、
public class MAIDLService extends Service {
private void Log(String str) {
Log.e("123", "----------" + str + "----------");
}
public void onCreate() {
Log("service created");
}
public void onStart(Intent intent, int startId) {
Log("service started id = " + startId);
}
public IBinder onBind(Intent t) {
Log("service on bind");
return mBinder;
}
public void onDestroy() {
Log("service on destroy");
super.onDestroy();
}
public boolean onUnbind(Intent intent) {
Log("service on unbind");
return super.onUnbind(intent);
}
public void onRebind(Intent intent) {
Log("service on rebind");
super.onRebind(intent);
}
PayAidlInterface.Stub mBinder = new PayAidlInterface.Stub() {
@Override
public int calculation(int anInt, int bnInt) throws RemoteException {
Log(anInt + "--" + bnInt);
return 1;
}
};
}
在来看下AndroidManifest.xml中MAIDLService 的配置:action是客户端调用用到的
<service android:name=".MAIDLService">
<intent-filter>
<action android:name="com.txy.umpay.aidl.MAIDLService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
服务端就已经完成了。接下来我们来看一下客户端的:
同样可以需要可服务端一样创建aidl文件

其实和服务端是一样的,把服务端的 PayAidlInterface.aidl 文件复制过来 再次执行 Build-->Make Module app
在来看下客户端怎么调用的
第一步先创建一个ServiceConnection 对象:
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
Log.e("123", "onServiceDisconnected:" + arg0.getPackageName());
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
Log.e("123", "onServiceConnected:" + name.getPackageName());
// 获取远程Service的onBinder方法返回的对象代理
service = PayAidlInterface.Stub.asInterface(binder);
}
};
第二步绑定:
//使用意图对象绑定开启服务
Intent intent = new Intent();
//在5.0及以上版本必须要加上这个
intent.setPackage("com.txy.umpay.aidl");
intent.setAction("com.txy.umpay.aidl.MAIDLService");//这个是上面service的action
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
第三步调用:
if(service != null){
int calculation = service.calculation(1, 2);
text.setText("calculation:"+calculation);
}
第四部不用的时候解除绑定:
@Override
protected void onDestroy () {
super.onDestroy();
if (mServiceConnection != null) {
unbindService(mServiceConnection);
}
}
下一篇文章:android studio 使用 aidl 异步回调
android studio 使用 aidl(一)基础用法的更多相关文章
- android studio 使用 aidl(二)异步回调
基础使用请移步 android studio 使用 aidl (一) 首先建立在server端建立两个aidl文件 ITaskCallback.aidl 用于存放要回调client端的方法 // IT ...
- 【转载】Android Studio Service AIDL 详解
公司产品之前IM这块存在很多问题,消息到达率低,加上协议上有些问题,丢消息频繁,所以需要重构IM,AIDL不能解决以上问题.好吧!那AIDL可以解决什么问题?什么是AIDL? 什么是AIDL? AID ...
- android studio 使用 aidl(三)权限验证
这篇文章是基于android studio 使用 aidl (一) 和 android studio 使用 aidl(二) 异步回调 下面的代码都是简化的,如果看不懂请先移步上2篇文章 网上的东西太坑 ...
- Android Studio中设置提示函数用法
Eclipse有一个很好的功能,就是当你代码调用某个android API时,鼠标移到对应的函数或者方法上,就会自动有一个悬浮窗提示该函数的说明(所包含的参数含义,该方法功能).迁移到Android ...
- Android Studio gradle 文件中 ${supportLibVersion} 用法
一般我们在项目中的gradle会添加如下库文件 dependencies { compile 'com.android.support:appcompat-v7:23.1.0' compile 'co ...
- Android Studio 之 控件基础知识
1. TextView 和 EditText 控件常用属性 android:layout_width="match_parent" 宽度与父控件一样宽 android:layou ...
- Android studio 中创建AIDL Service
1.概述 AIDL在android系统中的作用 AIDL,Android Interface definition language的缩写,它是一种android内部进程通信接口的描写叙述语言, ...
- android studio 的部分设置
1.android studio 如何提示方法的用法 在 Eclipse中鼠标放上去就可以提示方法的用法,实际上Android Studio也可以设置的.如图 Preferences > Edi ...
- Android Studio 之 NDK篇
由于工作内容的关系,对于NDK的工作涉及比较广(保密性,安全性),所以本章内容讲述一下NDK的基本使用过程. 网上也有很多这样的教程或者描述,但描述的并不完全 开发工具:Android Studio ...
随机推荐
- WPF_02_XAML
XAML(Extensible Application Markup Language的简写)是用于实例化.NET对象的标记语言.XAML对于WPF不是必须的. XAML基础 XAML标准: XAML ...
- TCP/IP概述(网络互联与TCP/IP)
TCP/IP概述(网络互联与TCP/IP) 用IP实现异构网络互联 从用户角度如何实现异构网络互联: 从用户角度看,实现异构网络互联的关键点就是使各种网络类型之间的差异对自己透明.在TCP/IP协议中 ...
- Spring Cloud 生产环境性能优化
先思考几个问题: 什么是百万并发连接? 什么是吞吐量? 操作系统能否支持百万连接? 操作系统维持百万连接需要多少内存? 应用程序维持百万连接需要多少内存? 百万连接的吞吐量是否超过了网络限制? 百万的 ...
- k8s入坑之路(13)kubernetes重要资源(namespace隔离 resources资源管理 label)
Namespace --- 集群的共享与隔离 语言中namespace概念 namespace核心作用隔离 以上是隔离的代码.namespace隔离的是: 1.资源对象的隔离:Service.Depl ...
- Mysql教程:(六)修改语句、、删除语句、字符查询like
1.修改语句 update 表名 set where 条件 mysql> update student set birth=1988,department='中文系' where id=901 ...
- KMP算法,看这篇就够了!
普通的模式匹配算法(BF算法) 子串的定位操作通常称为模式匹配算法 假设有一个需求,需要我们从串"a b a b c a b c a c b a b"中,寻找内容为"a ...
- LeetCode刷题 树专题
树专题 关于树的几个基本概念 1 树的节点定义 2 关于二叉树的遍历方法 2.1 前序遍历 2.2 中序遍历 2.3 后序遍历 2.4 层序遍历 3 几种常见的树介绍 3.1 完全二叉树 3.2 二叉 ...
- Linux usb 4. Device 详解
文章目录 1. 简介 2. Platform Layer 2.1 Platform Device 2.2 Platform Driver 3. UDC/Gadget Layer 3.1 Gadget ...
- Linux Mem (目录)
1.用户态相关: 1.1.用户态进程空间的创建 - execve() 详解 1.2.用户态进程空间的映射 - mmap()详解 1.3.分页寻址(Paging/MMU)机制详解 2.内核态相关: 2. ...
- C++概述及知识点总结
经过一段时间的学习,以前从没有接触过C++这个高逼格的语言的小白,逐渐对C++有了更深的了解和认识,C++是c语言的升级版,Bjarne Stroustrup在剑桥大学计算机中心工作.他使用过Simu ...