码迷,mamicode.com
首页 > 其他好文 > 详细

aidl和Binder解析

时间:2020-06-15 19:28:07      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:lte   obj   package   and   ati   method   ==   abs   ram   

先上实例代码:

service:

public class AidlService extends Service{

    @Override
    public IBinder onBind(Intent arg0) {
        /*return new AIDLTest.Stub() {

            @Override
            public String hello(String name) throws RemoteException {
                // TODO Auto-generated method stub
                return "hello"+name;
            }
        };*/
        return mBinder;
    }

    //一个类,继承了Binder,那么它的对象就可以被远程的进程使用了(前提是远程进程获取了这个类的对象【对象的引用】
    private final AIDLTest.Stub mBinder = new AIDLTest.Stub() {

        @Override
        public String hello(String name) throws RemoteException {
            // TODO Auto-generated method stub
            return "hello"+name;
        }
    };

 

client:

Intent intent = new Intent();
intent.setPackage("包名");

context.bindService(intent, serviceConnection,Context.BIND_AUTO_CREATE);
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "++++++ onServiceConnected: success ++++++");
vasInterface = AIDLTest.Stub.asInterface(service);
}

@Override
public void onServiceDisconnected(ComponentName name) {
vasInterface = null;
}
};

 

解析:

最主要的部分是AS自动生成的AIDLTest文件:

/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: F:\\Project\\Practice\\FrameworkDemo\\app\\src\\main\\aidl\\com\\example\\admin\\frameworkdemo\\AIDLTest.aidl
 */
package com.example.admin.frameworkdemo;

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

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

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

        private static class Proxy implements com.example.admin.frameworkdemo.AIDLTest {
            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 hello(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_hello, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readString();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }
        }

        static final int TRANSACTION_hello = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
    }

    public java.lang.String hello(java.lang.String name) throws android.os.RemoteException;
}

 

aidl和Binder解析

标签:lte   obj   package   and   ati   method   ==   abs   ram   

原文地址:https://www.cnblogs.com/wnpp/p/13135501.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!