博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android AIDL使用详解
阅读量:2394 次
发布时间:2019-05-10

本文共 2139 字,大约阅读时间需要 7 分钟。

aidl是 Android Interface definition language
是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口

操作步骤:
(1)在 src文件夹下 
创建 .aidl文件,在aidl文件定义如下:写法跟java代码类似
注:1> 它可以引用其它aidl文件中定义的接口,但是不能够引用你的java类文件中定义的接口
       2> 没有public等之类的修饰符
如:
package com.epsot.mobile.android.group.driver.aidl;
interface IRemoteGroupService {
  /** 
播放完成 
*/
  void onPlayCompletion();
  /** 
录音 
*/
  void onRecordVoice();
  /** 
获取群组名 
*/
  String onGetGroupName();
}
(2)编译aidl文件,这个只要是在eclipse中开发,你的adt插件会像资源文件一样把aidl文件编译成java代码生成在gen文件夹下,不用手动去编译:编译生成 
IRemoteGroupService
.java如我例子中代码
(3)
实现定义aidl接口中的内部抽象类 Stub,
Stub类继承了Binder,并继承我们在aidl文件中定义的接口,我们需要实现接口方法,下面是我在例子中实现的Stub类:
public
class
RemoteGroupService
extends
Service
{
   
@Override
   
public
void
onCreate()
   {
      
super
.onCreate();
   }
   
@Override
   
public
IBinder onBind(Intent intent)
   {
      
return
new
RemoteGroupServiceImpl();
   }
   
private
class
RemoteGroupServiceImpl
extends
IRemoteGroupService.Stub
   {
      
@Override
      
public
String onGetGroupName()
throws
RemoteException
      {
        
return
GroupApplication.
currentGroupName
;
      }
      
@Override
      
public
void
onPlayCompletion()
throws
RemoteException
      {
      }
      
@Override
      
public
void
onRecordVoice()
throws
RemoteException
      {
      }
   }
}
在Androidmanifest.xml
<
service
android:name
=
"com.epsot.mobile.android.group.driver.aidl.RemoteGroupService"
>
   
<
intent-filter
>
       
<
action
android:name
=
"com.epsot.mobile.android.group.driver.aidl.RemoteGroupService"
/>
       
<
action
android:name
=
"com.epsot.mobile.android.group.driver.aidl.RemoteGroupServiceMine"
/>
   
</
intent-filter
>
</
service
>
(4)在要使用的地方,如:
// 创建成员变量
private IRemoteGroupService mRemoteGroupService;
// 在合适的地方创建绑定服务,一般是在 onCreate 方法中
bindService( new Intent("com.epsot.mobile.android.group.driver.aidl.RemoteGroupService" ), mRemoteNavigationServiceConntection , Context.BIND_AUTO_CREATE );
private ServiceConnection mRemoteNavigationServiceConntection = new ServiceConnection()
 {
  @Override
  public void onServiceConnected(ComponentName name, IBinder service) 
{
      mRemoteGroupService = IRemoteGroupService.Stub.asInterface(service);
  }
  @Override
  public void onServiceDisconnected(ComponentName name) 
{
      mRemoteGroupService = null ;
  }
};
注:在同一个App中 IRemoteGroupService 可以找到,在不同的App则需要拥有相同路径名的aidl文件
你可能感兴趣的文章
gitee的使用
查看>>
maven项目出现如下错误,求指点;CoreException: Could not calculate build plan:
查看>>
理解Paxos算法的证明过程
查看>>
详解 JVM Garbage First(G1) 垃圾收集器
查看>>
Java 8 函数式编程入门之Lambda
查看>>
用高阶函数轻松实现Java对象的深度遍历
查看>>
WindowsApi+Easyx图形库的透明时钟
查看>>
Eclipse LUNA配置TomCat(非j2ee版本)
查看>>
树莓派安装mysql-srver报错 404 not found!
查看>>
Ubuntu 14.04LTS 下安装.net框架
查看>>
Eclipse 配置Groovy语言环境 && Java工程运行Groovy
查看>>
人工智能术语表
查看>>
Tensorflow Python API 翻译(sparse_ops)
查看>>
Tensorflow Python API 翻译(math_ops)(第一部分)
查看>>
Tensorflow Python API 翻译(math_ops)(第二部分)
查看>>
Tensorflow Python API 翻译(array_ops)
查看>>
Tensorflow Python API 翻译(constant_op)
查看>>
金融套利策略:理解统计套利的工作原理
查看>>
利用 TensorFlow 入门 Word2Vec
查看>>
课程 | 浅析数据标准化和归一化,优化机器学习算法输出结果
查看>>