AIDL 自定义实体类类型传递

AIDL实现传递自定义类型

  • 首先AIDL通过AS创建,会自动生成一个aidl文件夹和一个自己定义的.aidl文件
  • 在该文件下创建自定义的entity类型Book.java,并且实现Parcelable序列化

public class Book implements Parcelable {

    public Book(){

    }

    protected Book(Parcel in) {
        name = in.readString();
        age = in.readInt();
    }

    public static final Creator<Book> CREATOR = new Creator<Book>() {
        @Override
        public Book createFromParcel(Parcel in) {
            return new Book(in);
        }

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

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

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeString(name);
        out.writeInt(age);
    }
    
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}


  • 在aidl中需要import Book的包名和名字,并且需要创建一个Book.aidl,内容为parcelable Book;

package com.xxx.myapplication;

// Declare any non-default types here with import statements
import  com.xxx.myapplication.Book;

interface IBook {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */

   String getBookName(in Book book);
}



// Book.aidl
parcelable Book;

  • 把.aidl文件复制一份到客户端
  • 配置build.gradle

 sourceSets {
        main {
            java.srcDirs = ['src/main/java','src/main/aidl']
            aidl.srcDirs = ['src/main/aidl']
        }
    }

  • rebuild下生成.aidl的Stub java类

  • 编写服务端进程代码


public class RemoteService extends Service {


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }


    private IBook.Stub binder = new IBook.Stub() {
        @Override
        public String getBookName(Book book) throws RemoteException {
            return book.getName();
        }

    };
}

  • 编写客户端进程代码

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {

    private IBook iBook;

    private Button button, button2;
    private boolean mBound;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        button = findViewById(R.id.button);
        button2 = findViewById(R.id.button2);
        button.setOnClickListener(this);
        button2.setOnClickListener(this);
    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i("tag","connect");
            iBook = IBook.Stub.asInterface(service);
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e("tag","disconnect");
            mBound = false;
        }
    };


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                bindService(new Intent(this, RemoteService.class), connection, Context.BIND_AUTO_CREATE);
                break;
            case R.id.button2:
                if (mBound){
                    try {
                        Book book = new Book();
                        book.setName("客户端name");
                        book.setAge(2020);
                        Log.i("tag","getBookName: " + iBook.getBookName(book));
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
                break;
            default:
        }
    }
    
     @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }
}

  • 如上能编写没有报错,便可测试一下

你可能感兴趣的:(android)