Chinaunix首页 | 论坛 | 博客
  • 博客访问: 480013
  • 博文数量: 153
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 1724
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-08 11:55
文章分类

全部博文(153)

文章存档

2011年(1)

2010年(55)

2009年(88)

2008年(9)

我的朋友

分类: LINUX

2010-12-21 10:58:48



1). development/samples/ApiDemos/src/com/example/android/apis/app/
*.aidl, Localservice**.java  RemoteService*.java, ServiceStartArguments*.java

2). 深入解析Android 的AIDL Interface (高焕堂)


3). http://wzw19191.blog.163.com/blog/static/131135470201051675520369/

在android中,进程通讯是通过aidl机制来完成的。aidl模型如下:

                |<--------------------aidl---------------------->|
 client端 -->proxy  ----------parcel数据包-------- stub<---server端
也就是说:proxy+parcel+stub构成了aidl.只不过,proxy运行在客户进程,而 stub运行在服务端进程。当你通过aidl去访问服务端时,客户端会阻塞在proxy,服务端处理完后,通知proxy返回。

补充1:
应 用程序获取相关服务的代码:
private AlarmManager getAlarmManager() {
        synchronized (sSync) {
            if (sAlarmManager == null) {
                IBinder b = ServiceManager.getService(ALARM_SERVICE);
                IAlarmManager service = IAlarmManager.Stub.asInterface(b);
                sAlarmManager = new AlarmManager(service);
            }
        }
        return sAlarmManager;
    }


补充2:
AIDL文件生成的java文件中的Stub 类。
public static abstract class Stub extends android.os.Binder implements
            android.net.softap.ISoftApManager
{
        private static final java.lang.String DESCRIPTOR = "android.net.softap.ISoftApManager";

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

        /**
         * Cast an IBinder object into an ISoftApManager interface, generating a
         * proxy if needed.
         */
        public static android.net.softap.ISoftApManager asInterface(
                android.os.IBinder obj) {
            if ((obj == null)) {
                return null;
            }
            android.os.IInterface iin = (android.os.IInterface) obj
                    .queryLocalInterface(DESCRIPTOR);
            if (((iin != null) && (iin instanceof android.net.softap.ISoftApManager))) {
                return ((android.net.softap.ISoftApManager) iin);
            }
            return new android.net.softap.ISoftApManager.Stub.Proxy(obj);
        }

        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_getApEnabledState: {
                data.enforceInterface(DESCRIPTOR);
                int _result = this.getApEnabledState();
                reply.writeNoException();
                reply.writeInt(_result);
                return true;
            }
            case TRANSACTION_setApEnabled: {
                data.enforceInterface(DESCRIPTOR);
                boolean _arg0;
                _arg0 = (0 != data.readInt());
                boolean _result = this.setApEnabled(_arg0);
                reply.writeNoException();
                reply.writeInt(((_result) ? (1) : (0)));
                return true;
            }
            case TRANSACTION_setApSettings: {
                data.enforceInterface(DESCRIPTOR);
                android.net.softap.SoftApConfigure _arg0;
                if ((0 != data.readInt())) {
                    _arg0 = android.net.softap.SoftApConfigure.CREATOR
                            .createFromParcel(data);
                } else {
                    _arg0 = null;
                }
                boolean _result = this.setApSettings(_arg0);
                reply.writeNoException();
                reply.writeInt(((_result) ? (1) : (0)));
                return true;
            }
            case TRANSACTION_getApSettings: {
                data.enforceInterface(DESCRIPTOR);
                android.net.softap.SoftApConfigure _result = this
                        .getApSettings();
                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 android.net.softap.ISoftApManager {
            private android.os.IBinder mRemote;

            Proxy(android.os.IBinder remote) {
                mRemote = remote;
            }


            public android.os.IBinder asBinder() {
                return mRemote;
            }

            public java.lang.String getInterfaceDescriptor() {
                return DESCRIPTOR;
            }

            public int getApEnabledState() throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                int _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    mRemote.transact(Stub.TRANSACTION_getApEnabledState, _data,
                            _reply, 0);
                    _reply.readException();
                    _result = _reply.readInt();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }

            public boolean setApEnabled(boolean enable)
                    throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                boolean _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    _data.writeInt(((enable) ? (1) : (0)));
                    mRemote.transact(Stub.TRANSACTION_setApEnabled, _data,
                            _reply, 0);
                    _reply.readException();
                    _result = (0 != _reply.readInt());
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }

            public boolean setApSettings(
                    android.net.softap.SoftApConfigure datas)
                    throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                boolean _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    if ((datas != null)) {
                        _data.writeInt(1);
                        datas.writeToParcel(_data, 0);
                    } else {
                        _data.writeInt(0);
                    }
                    mRemote.transact(Stub.TRANSACTION_setApSettings, _data,
                            _reply, 0);
                    _reply.readException();
                    _result = (0 != _reply.readInt());
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }

            public android.net.softap.SoftApConfigure getApSettings()
                    throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                android.net.softap.SoftApConfigure _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    mRemote.transact(Stub.TRANSACTION_getApSettings, _data,
                            _reply, 0);
                    _reply.readException();
                    if ((0 != _reply.readInt())) {
                        _result = android.net.softap.SoftApConfigure.CREATOR
                                .createFromParcel(_reply);
                    } else {
                        _result = null;
                    }
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }
        }

        static final int TRANSACTION_getApEnabledState = (IBinder.FIRST_CALL_TRANSACTION + 0);
        static final int TRANSACTION_setApEnabled = (IBinder.FIRST_CALL_TRANSACTION + 1);
        static final int TRANSACTION_setApSettings = (IBinder.FIRST_CALL_TRANSACTION + 2);
        static final int TRANSACTION_getApSettings = (IBinder.FIRST_CALL_TRANSACTION + 3);
    }

补充3:
通常服务程序该Stub类,也即间接继承了 android.os.Binder, 进而可以作为 SystemManager.addService 等的参数而使用。
阅读(2976) | 评论(0) | 转发(0) |
0

上一篇:Security & Permission

下一篇:Package Signature

给主人留下些什么吧!~~