Android之——AIDL小结
来源:互联网 发布:剑网三萝莉脸型数据图 编辑:程序博客网 时间:2024/04/26 05:45
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47071683
- AIDL (Android Interface Definition Language )
- AIDL 适用于 进程间通信,并且与Service端多个线程并发的情况,如果只是单个线程 可以使用 Messenger ,如果不需要IPC 可以使用Binder
- AIDL语法:基础数据类型都可以适用,List Map等有限适用。static field 不适用。
- AIDL基本用法
1、实现.aidl文件
接口描述文件1、导入的包名2、如果有使用Object对象,需要该对象 implement Parcelable 接口,并且需要导入该接口包名+类名; 如果是primitive type 不需要这步。3、定义方法名称。4、所有的.aidl文件已经需要传递的对象接口需要在Service 与Client中各一份package com.aidl;import com.aidl.Data;interface IaidlData{ int getPid(); String getName(); com.aidl.Data getData();}
2、在Service中实现.aidl 接口。
实际实现的接口是在 gen中自动生成的 IaidlData.java的抽象内部类 Stub
1、需要在配置文件Androidmanifest.xml文件中声明Service,并且添加intent-filter 节点 的action属性, 此属性一般可以使用 aidl的包名+文件名(因为该值在服务器与客户端一致)2、需要实现IaidlData.aidl文件中定义的接口。 aidl文件是一个接口描述文件,会在gen中自动生成一个同名的IaidlData.java接口文件,该接口文件包含一个抽象类stub,其继承了android.os.Binder、实现IaidlData接口 故,我们实际需要实现的是Stub抽象类。public class AidlService extends Service{ public void onCreate() { Log.d("aidl", "aidlService--------------onCreate"); } public IBinder onBind(Intent intent) { return mBinder; } private final IaidlData.Stub mBinder = new IaidlData.Stub() { public int getPid() { return Process.myPid(); } public String getName() throws RemoteException { return "go or not go is a problem"; } public Data getData() throws RemoteException { Data data = new Data(); data.id = Process.myPid(); data.name = "go or not go is a problem"; return data; } };}
3、绑定Service ,并且获取IaidlService 对象
1、建立连接,使用Action属性定位需要的Service actoin的属性的采用aidl文件的类名+包名(与服务一致),之前需要在服务中设置相同的action属性,否则找不到服务。2、获取服务返回的stub对象,mIaidlData = IaidlData.Stub.asInterface(service);package com.clent;import com.aidl.IaidlData;import android.app.Activity;import android.content.ComponentName;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;public class AidlClientActivity extends Activity{ IaidlData mIaidlData; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } protected void onStart() { super.onStart(); Log.d("aidl" , "onstart ----------bindservice-----"+IaidlData.class.getName()); Intent intent = new Intent(IaidlData.class.getName()); bindService(intent, mSecondaryConnection, BIND_AUTO_CREATE); } private ServiceConnection mSecondaryConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { Log.d("aidl", "onServiceConnected----------------"); mIaidlData = IaidlData.Stub.asInterface(service); } public void onServiceDisconnected(ComponentName className) { mIaidlData = null; } }; public void onClick(View view) { System.out.println( " onclick--------------- : "); if(mIaidlData != null) { try { System.out.println( " name : "+mIaidlData.getName()); System.out.println( " id : "+mIaidlData.getPid()); System.out.println( " data : "+mIaidlData.getData().id +" "+mIaidlData.getData().name); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } protected void onDestroy() { super.onDestroy(); unbindService(mSecondaryConnection); }}
4、如果aidl文件中需要传递Object对象,需要添加对应的.aidl文件
1、定义该对象Data,并实现Parcelable2、添加Data.aidl文件,并采用如下方式编写导入Data3、需要在引用到Data的.aidl文件中 import com.aidl.Data4、需要在服务端和 客户端都添加 Data.aidl与 Data.java文件 并且需要一致。Data.aidlaidl文件:package com.aidl; parcelable Data;
5、添加 对应的Object类,并且实现Parcelable接口
public class Data implements Parcelable{ public int id; public String name; public static final Parcelable.Creator<Data> CREATOR = new Parcelable.Creator<Data>() { public Data createFromParcel(Parcel in) { return new Data(in); } public Data[] newArray(int size) { return new Data[size]; } }; public Data() { } private Data(Parcel in) { readFromParcel(in); } public void readFromParcel(Parcel in) { id = in.readInt(); name = in.readString(); } public int describeContents() { return 0; } public void writeToParcel(Parcel dest, int flags) { dest.writeInt(id); dest.writeString(name); }}
0 0
- Android之——AIDL小结
- AIDL之Android开发艺术探索小结
- android AIDL 小结
- android AIDL 小结
- android AIDL 小结
- android AIDL 小结
- Android之——AIDL深入
- Android进阶之路——AIDL
- Android开发艺术探索——IPC之Messenger、AIDL
- Android开发—— AIDL
- Android Service 之 AIDL
- Android之AIDL
- android 之 AIDL
- Android 之工具aidl
- Android之AIDL
- Android之AIDL使用
- Android之AIDL
- Android服务之AIDL
- 我不是蛤蟆!
- UVA 11400 - Lighting System Design(着重题意)
- C++ 中在进程中的窗口句柄传入线程
- java.lang.NoClassDefFoundError: org/objectweb/asm/Type
- leetcode-20-Valid Parentheses
- Android之——AIDL小结
- hiho1192 简单的树嵌入(构造题)
- hdu 1533 Going Home (最小费用最大流)
- 杨辉三角_java_HDU2032
- 内存管理
- SSL/TLS双向认证实现(JAVA、TOMCAT)
- Fine-Grained Histopathological Image Analysis via Robust Segmentation and Large-Scale Retrieval文章总结
- 分离各位数
- 找数达人