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 ...
随机推荐
- 国产新芯片连不上J-Link?芯海CS32L010系列芯片JLink配置方法
疫情以来芯片供货紧张,特别是ST的MCU一芯难求.所以很多产品不得不切换成国产.不过也是经过使用后才发现,很多国产芯片的性能还是挺好的.由于芯片比较新,官方J-Link还没有支持,所以调试和烧录有些不 ...
- 三层组网AP上线外接DHCP
一.实验目的 在3-1的基础上增加DHCP的配置方法 二.实验仪器设备及软件 仪器设备:一台AC,四台AP,一台路由充当DHCP服务器 软件:ENSP 三.实验原理 四. 实验内容与步骤 1.三层 ...
- redis开外网访问
Redis: 注释掉bind 127.0.0.1可以使所有的ip访问redis 若是想指定多个ip访问,但并不是全部的ip访问,可以bind protected-mode no /etc/init.d ...
- node 中第三方模块的加载过程原理
node 中第三方模块的加载过程原理 凡是第三方模块都必须通过 npm 来下载 使用的时候就可以通过require('包名') 的方式来进行加载才可以使用 不可能有任何一个第三方包和核心模块的名字是一 ...
- C# | VS2019连接MySQL的三种方法以及使用MySQL数据库教程
本文将介绍3种添加MySQL引用的方法,以及连接MySQL和使用MySQL的教程 前篇:Visual Studio 2019连接MySQL数据库详细教程 \[QAQ \] 第一种方法 下载 Mysql ...
- 第九届网安竞赛writeup
web easysql[已解决] uname=a') union select 1,extractvalue(1, concat(0x7e, (select database()),0x7e))#&a ...
- Python基础(数据类型与变量、字符串和编码)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # name = 200 # if name > 100: # print(name,'大于100' ...
- eclipse下的python环境安装
添加python开发环境到eclipse: 点击help--install New Software 点击add,弹出新窗口: Name:填PyDev Location:填 http://pyde ...
- 使用 docker + verdaccio 搭建npm私有仓库
本文介绍如何使用 verdaccio 搭建私有npm仓库,以及使用 docker 时如何映射到本地目录,方便简单对仓库进行各种操作.系统环境是 Linux. verdaccio verdaccio 是 ...
- soname and real name
[1] https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes#Backward_compatibility [2] https://akka ...