美文网首页
Android-AIDL使用

Android-AIDL使用

作者: 荞麦穗 | 来源:发表于2020-05-16 20:49 被阅读0次

AIDL使用,多进程内的多线程情况

1、在src的main目录下创建AIDL文件夹,Android studio 会直接插创建一个.aidl文件,如IMyAidlInterface.aidl
interface IMyAidlInterface {
    //手动添加的要使用的方法
    String getName();
    //声明可以直接传递的对象,可以删除
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}

Sync后会在gen内生成一个IMyAidlInterface.java 文件

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

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

        /**
         * Cast an IBinder object into an com.provider.demo.providerdemo.IMyAidlInterface interface,
         * generating a proxy if needed.
         */
        public static com.provider.demo.providerdemo.IMyAidlInterface asInterface(android.os.IBinder obj) {
            if ((obj == null)) {
                return null;
            }
            android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
            if (((iin != null) && (iin instanceof com.provider.demo.providerdemo.IMyAidlInterface))) {
                return ((com.provider.demo.providerdemo.IMyAidlInterface) iin);
            }
            return new com.provider.demo.providerdemo.IMyAidlInterface.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_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.provider.demo.providerdemo.IMyAidlInterface {
            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 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_getName = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
    }

    public java.lang.String getName() throws android.os.RemoteException;
}
2、创建RemoteService.java
public class RemoteService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
    final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() {
        @Override
        public String getName() throws RemoteException {
            return "Binder Source Code very difficult";
        }
    };
}

在Manifest文件里注册

<service android:name=".RemoteService"
    android:process=":remote">//另起一个进程,它会再次生成一个Application文件,所以在一个应用中,Application文件是不唯一的
    <intent-filter>
        <action android:name="com.provider.demo.providerdemo.RemoteService" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</service>

到此,AIDL为Service段创建完毕

二、client 调用

1、如果是不同的应用,要保证IMyAidlInterface.aidl文件的内容和包名完全一致如,直接把aidl文件夹拷贝到新的工程中,
同样的会在gen内部生成IMyAidlInterface.java文件,便于asInterface()方法传递服务端的BinderProxy对象
2、创建ServiceConnection对象,接收远程的SMP
IMyAidlInterface mRemote;


private ServiceConnection serviceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mRemote = IMyAidlInterface.Stub.asInterface(service);
        try {
            String info = mRemote.getName();
            Log.i("cxd", "onServiceConnected: "+info);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
};

在onStart()方法内部绑定服务

Intent intent = new Intent("com.provider.demo.providerdemo.RemoteService");
intent.setPackage(getPackageName());
bindService(intent,serviceConnection,Context.BIND_AUTO_CREATE);

相关文章

网友评论

      本文标题:Android-AIDL使用

      本文链接:https://www.haomeiwen.com/subject/wictnqtx.html