Android AIDL理解
来源:互联网 发布:用jsp简单游戏编程 编辑:程序博客网 时间:2024/05/17 09:34
Android AIDL理解
AILD(Android Interface definition language) 安卓接口定义语言
很显然,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口。
编译器通过*.aidl文件的描述信息生成符合通信协议的Java代码,我们无需自己去写这段繁杂的代码,只需要在需要的时候调用即可,通过这种方式我们就可以完成进程间的通信工作。
*.aidl文件的写法和java中interface一样,但是我们需要注意的是:
它可以引用其它aidl文件中定义的接口,但是不能够引用你的java类文件中定义的接口。
编译你的aidl文件,你的adt插件会像资源文件一样把aidl文件编译成java代码生成在gen文件夹下。
下面是一个简单的小案例:
很明显在我们跟目录下自动生成了我们的Person.java
那么 我们的Person.aidl究竟是什么样的呢
package com.jeason.aidldemo;interface Person{String speak(String name);void eat();}
编译过程我们自动生成的呢
/* * This file is auto-generated. DO NOT MODIFY. * Original file: D:\\AndroidData\\Lock_Screen\\AIDLDemo\\src\\com\\jeason\\aidldemo\\Person.aidl */package com.jeason.aidldemo;public interface Person extends android.os.IInterface{/** Local-side IPC implementation stub class. */public static abstract class Stub extends android.os.Binder implements com.jeason.aidldemo.Person{private static final java.lang.String DESCRIPTOR = "com.jeason.aidldemo.Person";/** Construct the stub at attach it to the interface. */public Stub(){this.attachInterface(this, DESCRIPTOR);}/** * Cast an IBinder object into an com.jeason.aidldemo.Person interface, * generating a proxy if needed. */public static com.jeason.aidldemo.Person asInterface(android.os.IBinder obj){if ((obj==null)) {return null;}android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);if (((iin!=null)&&(iin instanceof com.jeason.aidldemo.Person))) {return ((com.jeason.aidldemo.Person)iin);}return new com.jeason.aidldemo.Person.Stub.Proxy(obj);}@Override public android.os.IBinder asBinder(){return this;}@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException{switch (code){case INTERFACE_TRANSACTION:{reply.writeString(DESCRIPTOR);return true;}case TRANSACTION_speak:{data.enforceInterface(DESCRIPTOR);java.lang.String _arg0;_arg0 = data.readString();java.lang.String _result = this.speak(_arg0);reply.writeNoException();reply.writeString(_result);return true;}case TRANSACTION_eat:{data.enforceInterface(DESCRIPTOR);this.eat();reply.writeNoException();return true;}}return super.onTransact(code, data, reply, flags);}private static class Proxy implements com.jeason.aidldemo.Person{private android.os.IBinder mRemote;Proxy(android.os.IBinder remote){mRemote = remote;}@Override public android.os.IBinder asBinder(){return mRemote;}public java.lang.String getInterfaceDescriptor(){return DESCRIPTOR;}@Override public java.lang.String speak(java.lang.String name) throws android.os.RemoteException{android.os.Parcel _data = android.os.Parcel.obtain();android.os.Parcel _reply = android.os.Parcel.obtain();java.lang.String _result;try {_data.writeInterfaceToken(DESCRIPTOR);_data.writeString(name);mRemote.transact(Stub.TRANSACTION_speak, _data, _reply, 0);_reply.readException();_result = _reply.readString();}finally {_reply.recycle();_data.recycle();}return _result;}@Override public void eat() throws android.os.RemoteException{android.os.Parcel _data = android.os.Parcel.obtain();android.os.Parcel _reply = android.os.Parcel.obtain();try {_data.writeInterfaceToken(DESCRIPTOR);mRemote.transact(Stub.TRANSACTION_eat, _data, _reply, 0);_reply.readException();}finally {_reply.recycle();_data.recycle();}}}static final int TRANSACTION_speak = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);static final int TRANSACTION_eat = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);}public java.lang.String speak(java.lang.String name) throws android.os.RemoteException;public void eat() throws android.os.RemoteException;}
用目录的形式展示就是
很显然的我们发现 我们的aidl生成的java文件只是在我们的方法上增加了Stub这个抽象类
而Stub继承了android.os.Binder,那么毫无疑问,他就是我们进程之件通信的核心
而在Stub中我们发现asInterface这个方法将IBinder类型的对象转换成Person类型,必要的时候生成一个代理对象返回结果。这个代理对象就是Proxy
return new com.jeason.aidldemo.Person.Stub.Proxy(obj);........private static class Proxy implements com.jeason.aidldemo.Person......
而真正执行我们的函数的是onTransact,并且他是被代理Proxy调用
package com.jeason.aidlservice;import com.jeason.aidldemo.Person;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;public class MainAidlService extends Service{ Person.Stub mPerson = new Person.Stub() { @Override public String speak(String name) throws RemoteException { // TODO Auto-generated method stub Log.d("Jeason","Person -> speak()"); return "My name is"+name; } @Override public void eat() throws RemoteException { // TODO Auto-generated method stub Log.d("Jeason","Person -> eat()"); } }; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub Log.d("Jeason","MainAidlService onBind"); return mPerson; }}
这列很明显的发现我们的Service 用来传递消息
package com.jeason.aidl;import com.jeason.aidldemo.Person;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainAidlActivity extends Activity implements OnClickListener { private Button bt1,bt2; private Person person = null; private ServiceConnection conn = new ServiceConnection(){ @Override public void onServiceConnected(ComponentName arg0, IBinder service) { // TODO Auto-generated method stub Log.d("Jeason","ServiceConnection"); person = Person.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName arg0) { // TODO Auto-generated method stub } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_aidl); Intent intent = new Intent("android.intent.action.AIDLService"); bindService(intent, conn, Context.BIND_AUTO_CREATE); bt1 = (Button)findViewById(R.id.xx); bt1.setOnClickListener(this); bt2 = (Button)findViewById(R.id.zz); bt2.setOnClickListener(this); } @Override public void onClick(View view) { // TODO Auto-generated method stub if(view == bt1){ try { String retVal = person.speak("Jeason"); Toast.makeText(getApplicationContext(), retVal, Toast.LENGTH_SHORT).show(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(view == bt2){ try { person.eat(); Toast.makeText(getApplicationContext(), "eat", Toast.LENGTH_SHORT).show(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
顺便给出我们的
<service android:name="com.jeason.aidlservice.MainAidlService" > <intent-filter> <action android:name="android.intent.action.AIDLService" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service>
0 0
- android AIDL 自我理解
- Android-深入理解AIDL
- Android AIDL理解
- Android-深入理解AIDL
- Android AIDL理解
- Android AIDL的重新理解
- android aidl接口的理解
- 【Android】由浅到深理解AIDL
- android bind aidl 实例理解
- 初步理解android中的aidl
- Android AIDL 理解及开发要点
- Android AIDL 理解及开发要点
- android 进程间通信 aidl 简单理解
- Android AIDL的理解和使用
- Android 中AIDL的使用与理解
- 从android aidl理解Proxy/stub模式
- AIDL的android:process的理解
- [Android]AIDL中的in,out,inout理解
- SpringMVC中使用Interceptor拦截器
- ProgressDialog用法详解
- VC,MFC中通过POST与GET方式与WEB服务器通信
- 25个漂亮的响应式布局的web设计
- 机器学习与深度学习资料
- Android AIDL理解
- Android程序:TimePicker和DatePicker的两种实现案例
- Facebook POP 进阶指南
- Android nfc模块读写MifareClassic卡50
- listctrl 插入多条数据时避免闪烁
- PHP的静态变量介绍
- C++ 版 Longest Common Prefix leetcode提交正确。
- JUnit结果重跑失败用例(支持Mvn和Ant)
- make编译报错 "Makefile", line 10: make: Dependency line needs colon or double colon operator.