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 ...
随机推荐
- windows 系统文件夹挂载到 Linux 系统,拷贝(发送)文件到 windows 系统,实现异地备份
1.在windows 系统上配置好共享文件夹,用来接收Linux 系统的文件 注意:关闭windows 系统防火墙,或者添加进出站规则 2.在Linux 系统中,创建需要拷贝的文件目录 #mkdi ...
- upload-labs通关攻略(全)
upload-labs通关攻略 upload-labs是练习文件上传很好的一个靶场,建议把upload-labs关卡全部练习一遍 1.下载安装 下载地址 链接:https://pan.baidu.co ...
- SpringBoot2.x异步任务EnableAsync
1.springboot启动类里面使用@EnableAsync注解开启异步功能 @EnableAsync public class Demo001Application { public static ...
- 【Java】IO流
File类 介绍 File类的一个对象,代表一个文件或一个文件目录 File类声明在java.io包下 File类中涉及关于文件或文件目录的创建.删除.重命名.修改时间.文件大小等方法,并未涉及到写入 ...
- mybatis之参数传递的方式 | mybatis
1.单个参数(基本类/包装类+String) 这种情况MyBatis可直接使用这个参数,不需要经过任何处理. 一个参数情况下#{}中内容随便写 public Employee getEmployeeB ...
- C++中简单使用HP-Socket
目录 简介 使用方式 实现简单线程池 实现TCP客户端 实现TCP服务端 实现Http客户端 附件 简介 HP-Socket 是一套通用的高性能 TCP/UDP /HTTP 通信 框架 ,包含服务端组 ...
- django test, app aren't loaded yet
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. 解决方法: 用django的TestCase from djan ...
- 快速排序平均时间复杂度O(nlogn)的推导
快速排序作为随机算法的一种,不能通过常规方法来计算时间复杂度 wiki上有三种快排平均时间复杂度的分析,本文记录了一种推导方法. 先放快速排序的伪代码,便于回顾.参考 quicksort(int L, ...
- Effective C++ 总结笔记(四)
五.实现 26.尽可能延后变量定义式的出现时间 尽可能延后变量定义式的出现,甚至应该尝试延后这份定义直到能够给他初值实参为止,这样不仅能避免构造和析构非必要对象,避免无意义的default行为,也可增 ...
- dotNET5的MVC页面传值方式总结
本文大致讲解mvc前后端的传值方式,包括control向view.view向control.以及action向action. 一.经典回顾 二.Controller向View传值 1. ViewBag ...