美文网首页
Android---AIDL-IPC通信

Android---AIDL-IPC通信

作者: this_is_for_u | 来源:发表于2017-06-18 16:45 被阅读30次

    参考资料http://liuwangshu.cn/application/ipc/3-aidl.html

    创建AIDL文件

    image.png

    IBookManager.aidl

    package megvii.ipc;
    //使用了Book类,这个类实现了Parcelable,需要import进来
    import megvii.ipc.Book;
    //所在包包名应该与应用包名一致
    interface IBookManager {
        void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
                double aDouble, String aString);
    
        List<Book>getBook();
        void addBook(in Book book);
    }
    
    
    package megvii.ipc;
    
    import android.os.Parcel;
    import android.os.Parcelable;
    
    /**
     * Created by wangzhiqiang on 2017/6/18.
     */
    
    public class Book implements Parcelable {
    
        String book;
        String des;
    
        public Book(String book, String des){
            this.book = book;
            this.des = des;
        }
        public Book(Parcel in){
            book = in.readString();
            des = in.readString();
        }
    
        public static final Creator<Book> CREATOR = new Creator<Book>() {
            @Override
            public Book createFromParcel(Parcel source) {
                return new Book(source);
            }
    
            @Override
            public Book[] newArray(int size) {
                return new Book[size];
            }
        };
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(book);
            dest.writeString(des);
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    }
    
    
    package megvii.ipc;
    parcelable Book;
    //要创建Book.aidl,声明Book实现了parceable接口
    //声明完所有aidl接口并写完Book.java后重新编译项目,就会生成与aidl相对应的接口文件
    
    image.png
    package megvii.ipc;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    import android.os.RemoteException;
    import android.support.annotation.Nullable;
    
    import java.util.List;
    import java.util.concurrent.CopyOnWriteArrayList;
    
    /**
     * Created by wangzhiqiang on 2017/6/18.
     */
    
    public class AIDLService extends Service {
    
        private CopyOnWriteArrayList<Book> mBook = new CopyOnWriteArrayList<>();
    
        private Binder mBinder = new IBookManager.Stub(){
            @Override
            public void addBook(Book book) throws RemoteException {
                mBook.add(book);
            }
    
            @Override
            public List<Book> getBook() throws RemoteException {
                return mBook;
            }
    
            @Override
            public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
    
            }
        };
    
        @Override
        public void onCreate() {
            super.onCreate();
            mBook.add(new Book("1","2"));
        }
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return mBinder;
        }
    }
    
    
    private ServiceConnection aidlConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                //将绑定成功后的Binder对象转换为aidl接口,通过这个接口调用远程服务端方法
                IBookManager iBookManager = IBookManager.Stub.asInterface(service);
                //绑定成功后创建新的Book然后调用服务端addBook方法添加,打印
                Book book = new Book("nihao", "hello");
                try{
                    iBookManager.addBook(book);
                    List<Book> mbook =iBookManager.getBook();
                    for (int i = 0; i < mbook.size(); i++){
                        Book bo = mbook.get(i);
                        Log.i(TAG, bo.book + "----" + bo.des);
                    }
                }catch (RemoteException e){
                    e.printStackTrace();
                }
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {
    
            }
        };
    
     @Override
        protected void onDestroy() {
            super.onDestroy();
        //    unbindService(mService);
            unbindService(aidlConnection);
        }
    
    Intent intent = new Intent(MainActivity.this, AIDLService.class);
            bindService(intent, aidlConnection, Context.BIND_AUTO_CREATE);
    
    <service android:name=".AIDLService"
                android:process=":aidlservice"/>
    

    相关文章

      网友评论

          本文标题:Android---AIDL-IPC通信

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