Android应用开发----AIDL的使用

一: 创建AIDL文件    IMyService.aidl

AIDL文件基本数据类型:(int, long, char, boolean等),String和CharSequence,集合接口类型List和Map,不需要import 语句。

Parcelable数据类型: 需要有方向指示,in表示由客户端设置,out表示由服务端设置,inout是两者均可设置。需要import这些类


package com.demo;

import com.demo.Person;

interface IMyService {
        void savePersonInfo(in Person person);
        List<Person> getAllPerson();
}


二: Parcelable文件定义

1- implements Parcelable

2- 实现三个函数

将类数据写入序列化对象dest

 @Override
        public void writeToParcel(Parcel dest, int flags) {
                dest.writeString(a a a);
                dest.writeString(b b b);
                dest.writeInt(c c c);
        }

不需要修改

@Override
        public int describeContents() {
                return 0;
        }


不需要修改,构造器

public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {

                @Override
                public Person createFromParcel(Parcel source) {
                        return new Person(source);
                }

                @Override
                public Person[] newArray(int size) {
                        return new Person[size];
                }

        };

三:service代码编写

public class RemoteService extends Service {

        private LinkedList<Person> personList = new LinkedList<Person>();
        
        @Override
        public IBinder onBind(Intent intent) {
                return mBinder;
        }

        private final IMyService.Stub mBinder = new IMyService.Stub(){

                @Override
                public void savePersonInfo(Person person) throws RemoteException {
                        if (person != null){
                                personList.add(person);
                        }
                }

                @Override
                public List<Person> getAllPerson() throws RemoteException {
                        return personList;
                }
        };
}


四:使用

Server端:

<service
            android:name="com.example.aidlserver.AidlServerService">
            <intent-filter>
                <action android:name="forServiceAidl"/>
            </intent-filter>
</service>

使用binder连接,调用函数

bindService(new Intent("forServiceAidl"), sc, Service.BIND_AUTO_CREATE);

client端:

将server端的aidl文件,Parcelable文件copy到client端

然后,binder连接service

bindService(new Intent("forServiceAidl"), sc, Service.BIND_AUTO_CREATE);


IPerson.Stub.asInterface(binder);返回的就是可以操作的service对象



五:aidl文件生成的java文件解析

<span style="font-family:SimSun;font-size:14px;color:#333333;">/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: /home/user/workspace/AidlServer/src/com/example/aidldefine/IPerson.aidl
 */
package com.example.aidldefine;

public interface IPerson extends android.os.IInterface {
	/** Local-side IPC implementation stub class. */
	public static abstract class Stub extends android.os.Binder implements
			com.example.aidldefine.IPerson {
		private static final java.lang.String DESCRIPTOR = "com.example.aidldefine.IPerson";

		/** Construct the stub at attach it to the interface. */
		public Stub() {
			this.attachInterface(this, DESCRIPTOR);
		}

		/**
		 * Cast an IBinder object into an com.example.aidldefine.IPerson
		 * interface, generating a proxy if needed.
		 */
		public static com.example.aidldefine.IPerson asInterface(
				android.os.IBinder obj) {
			if ((obj == null)) {
				return null;
			}
			android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
			if (((iin != null) && (iin instanceof com.example.aidldefine.IPerson))) {
				return ((com.example.aidldefine.IPerson) iin);
			}
			return new com.example.aidldefine.IPerson.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_setName: {
				data.enforceInterface(DESCRIPTOR);
				java.lang.String _arg0;
				_arg0 = data.readString();
				this.setName(_arg0);
				reply.writeNoException();
				return true;
			}
			case TRANSACTION_getName: {
				data.enforceInterface(DESCRIPTOR);
				java.lang.String _result = this.getName();
				reply.writeNoException();
				reply.writeString(_result);
				return true;
			}
			}
			return super.onTransact(code, data, reply, flags);
		}

		private static class Proxy implements com.example.aidldefine.IPerson {
			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 void setName(java.lang.String name)
					throws android.os.RemoteException {
				android.os.Parcel _data = android.os.Parcel.obtain();
				android.os.Parcel _reply = android.os.Parcel.obtain();
				try {
					_data.writeInterfaceToken(DESCRIPTOR);
					_data.writeString(name);
					mRemote.transact(Stub.TRANSACTION_setName, _data, _reply, 0);
					_reply.readException();
				} finally {
					_reply.recycle();
					_data.recycle();
				}
			}

			@Override
			public java.lang.String getName() 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);
					mRemote.transact(Stub.TRANSACTION_getName, _data, _reply, 0);
					_reply.readException();
					_result = _reply.readString();
				} finally {
					_reply.recycle();
					_data.recycle();
				}
				return _result;
			}
		}

		static final int TRANSACTION_setName = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
		static final int TRANSACTION_getName = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
	}

	public void setName(java.lang.String name)
			throws android.os.RemoteException;

	public java.lang.String getName() throws android.os.RemoteException;
}
</span>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值