赞
踩
关于AIDL是什么,包括AIDL用途等概念性,可以网上搜一下,一大堆,在这里我直接说一下AIDL的用法,AIDL总起来说就是两个APK在不同的操作空间通信,传递数据,传递数据分为两种:
默认类型:Int float 等八大类型
非默认类型:自定义类继承自Parcelable
这篇博客先说一下 传递默认类型数据。
需求:有一个APP(服务端)内有一个类是用于计算两个数的和,还有一个APP(客户端)用户向服务端发送两个int型数据,然后服务端计算之后再返回给客户端,客户端展示在UI上。
先看下服务器APP的代码:
1:创建工程,然后在app上创建aidl文件,创建之后可以看到是跟Java是同一等级的,如下图;
2:在IMyAidlInterface.aidl文件内添加如下代码:
// IMyAidlInterface.aidl
package com.app.test.aidlfirstproject;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
int add(int a, int b);
}
3:创建ServerService.java类继承自Service,代码如下:
package com.app.test.aidlfirstproject;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
/**
* Created by ${liumengqiang} on 2017/9/15.
*/
public class ServerService extends Service{
@Nullable
@Override
public IBinder onBind(Intent intent) {
return stub;
}
IMyAidlInterface.Stub stub = new IMyAidlInterface.Stub() {
@Override
public int add(int a, int b) throws RemoteException {

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。