服务 远程服务 AIDL 进程间通讯 IPC 深化
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了服务 远程服务 AIDL 进程间通讯 IPC 深化相关的知识,希望对你有一定的参考价值。
示例
aidl接口文件
package com.bqt.aidlservice.aidl;parcelable Person;package com.bqt.aidlservice.aidl;parcelable Salary;package com.bqt.aidlservice.aidl;import com.bqt.aidlservice.aidl.Salary;import com.bqt.aidlservice.aidl.Person;interface IBinderInterface {void callMethodInService();String queryPerson(int num);//定义一个Person对象作为传入参数。接口中定义方法时,需要制定新参的传递模式,这里是传入,所以前面有一个【in】Salary getMsg(in Person owner);}
Bean1
package com.bqt.aidlservice.aidl;import android.os.Parcel;import android.os.Parcelable;public class Person implements Parcelable {public String name;public Person(String name) {this.name = name;}@Overridepublic int hashCode() {return name.hashCode();}@Overridepublic boolean equals(Object o) {if (o instanceof Person) {return name.equals(((Person) o).name);} else {return false;}}@Overridepublic int describeContents() { //实现Parcelable必须实现的方法,不知道拿来干嘛的,直接返回0就行了return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) { //把对象所包含的数据写入到parcel中dest.writeString(name);//可以write各种类型的数据}//必须提供一个名为【CREATOR】的static final属性,该属性需要实现android.os.Parcelable.Creator<T>接口public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {@Overridepublic Person createFromParcel(Parcel source) {return new Person(source.readString());//从Parcel中读取数据,返回Person对象,和构造方法中的参数列表相对应}@Overridepublic Person[] newArray(int size) {return new Person[size];}};}
Bean2
package com.bqt.aidlservice.aidl;import android.os.Parcel;import android.os.Parcelable;public class Salary implements Parcelable {public String type;public Integer salary;public Salary(String type, Integer salary) {this.type = type;this.salary = salary;}public String toString() {return "工作:" + type + " 薪水: " + salary;}@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeString(type);dest.writeInt(salary);}public static final Parcelable.Creator<Salary> CREATOR = new Parcelable.Creator<Salary>() {//从Parcel中读取数据,返回Person对象@Overridepublic Salary createFromParcel(Parcel source) {return new Salary(source.readString(), source.readInt());}@Overridepublic Salary[] newArray(int size) {return new Salary[size];}};}
自动生成的Java接口文件
/** This file is auto-generated. DO NOT MODIFY. Original file:* D:\\\\workspace_bqt\\\\* ??????\\\\src\\\\com\\\\bqt\\\\aidlservice\\\\aidl\\\\IBinderInterface.aidl*/package com.bqt.aidlservice.aidl;public interface IBinderInterface extends android.os.IInterface {/** Local-side IPC implementation stub class. */public static abstract class Stub extends android.os.Binder implements com.bqt.aidlservice.aidl.IBinderInterface {private static final java.lang.String DESCRIPTOR = "com.bqt.aidlservice.aidl.IBinderInterface";/** Construct the stub at attach it to the interface. */public Stub() {this.attachInterface(this, DESCRIPTOR);}/*** Cast an IBinder object into an com.bqt.aidlservice.aidl.IBinderInterface interface,* generating a proxy if needed.*/public static com.bqt.aidlservice.aidl.IBinderInterface asInterface(android.os.IBinder obj) {if ((obj == null)) {return null;}android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);if (((iin != null) && (iin instanceof com.bqt.aidlservice.aidl.IBinderInterface))) {return ((com.bqt.aidlservice.aidl.IBinderInterface) iin);}return new com.bqt.aidlservice.aidl.IBinderInterface.Stub.Proxy(obj);}@Overridepublic android.os.IBinder asBinder() {return this;}@Overridepublic 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_callMethodInService: {data.enforceInterface(DESCRIPTOR);this.callMethodInService();reply.writeNoException();return true;}case TRANSACTION_queryPerson: {data.enforceInterface(DESCRIPTOR);int _arg0;_arg0 = data.readInt();java.lang.String _result = this.queryPerson(_arg0);reply.writeNoException();reply.writeString(_result);return true;}case TRANSACTION_getMsg: {data.enforceInterface(DESCRIPTOR);com.bqt.aidlservice.aidl.Person _arg0;if ((0 != data.readInt())) {_arg0 = com.bqt.aidlservice.aidl.Person.CREATOR.createFromParcel(data);} else {_arg0 = null;}com.bqt.aidlservice.aidl.Salary _result = this.getMsg(_arg0);reply.writeNoException();if ((_result != null)) {reply.writeInt(1);_result.writeToParcel(reply, android.os.Parcelable.PARCELABLE_WRITE_RETURN_VALUE);} else {reply.writeInt(0);}return true;}}return super.onTransact(code, data, reply, flags);}private static class Proxy implements com.bqt.aidlservice.aidl.IBinderInterface {private android.os.IBinder mRemote;Proxy(android.os.IBinder remote) {mRemote = remote;}@Overridepublic android.os.IBinder asBinder() {return mRemote;}public java.lang.String getInterfaceDescriptor() {return DESCRIPTOR;}@Overridepublic void callMethodInService() 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_callMethodInService, _data, _reply, 0);_reply.readException();} finally {_reply.recycle();_data.recycle();}}@Overridepublic java.lang.String queryPerson(int num) 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.writeInt(num);mRemote.transact(Stub.TRANSACTION_queryPerson, _data, _reply, 0);_reply.readException();_result = _reply.readString();} finally {_reply.recycle();_data.recycle();}return _result;}//定义一个Person对象作为传入参数。接口中定义方法时,需要制定新参的传递模式,这里是传入,所以前面有一个【in】@Overridepublic com.bqt.aidlservice.aidl.Salary getMsg(com.bqt.aidlservice.aidl.Person owner) throws android.os.RemoteException {android.os.Parcel _data = android.os.Parcel.obtain();android.os.Parcel _reply = android.os.Parcel.obtain();com.bqt.aidlservice.aidl.Salary _result;try {_data.writeInterfaceToken(DESCRIPTOR);if ((owner != null)) {_data.writeInt(1);owner.writeToParcel(_data, 0);} else {_data.writeInt(0);}mRemote.transact(Stub.TRANSACTION_getMsg, _data, _reply, 0);_reply.readException();if ((0 != _reply.readInt())) {_result = com.bqt.aidlservice.aidl.Salary.CREATOR.createFromParcel(_reply);} else {_result = null;}} finally {
以上是关于服务 远程服务 AIDL 进程间通讯 IPC 深化的主要内容,如果未能解决你的问题,请参考以下文章
客户端进程(在带有aidl的android IPC中)如何知道远程服务器类?