AIDL中的in out inout

AIDL中的in out inout,官方是这样介绍的,

All non-primitive parameters require a directional tag indicating which way the data goes . Either in , out , or inout . Primitives are in by default , and connot be otherwise .

说的很抽象,我的理解是 如果客户端调用服务端实现的接口,在服务端修改参数的属性的话 对客户端原来的对象如果同步修改的话 就需要设置为out/inout,如果修改客户端的参数的属性不影响原来的对象,就用in

interface StudentManager {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
    void addStudentInout(inout Student s);//
    void addStudentIn(in Student s);
    //void addStudentOut(out Student s);//声明为out的话 一直报错 不知道什么原因
    List getStudents();
    void clear(int isClear);
}
public class StudentService extends Service {
    private List students = new ArrayList<>();

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

    /**
     * 实现StudentManager接口
     */
    private StudentManager.Stub binder = new StudentManager.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

        @Override
        public void addStudentInout(Student s) throws RemoteException {
            s.setName("update");//修改name属性 客户端s对象的name属性做响应的修改
            students.add(s);
        }

        @Override
        public void addStudentIn(Student s) throws RemoteException {
            s.setName("update");//修改name属性 不会影响客户端对象的属性
            students.add(s);
        }

/*
        @Override
        public void addStudentOut(Student s) throws RemoteException {
            students.add(s);//如果接口中申明为out 参数的话一直报错 不能通过编译,暂时不知道什么原因
        }
*/

        @Override
        public List getStudents() throws RemoteException {
            return students;
        }

        @Override
        public void clear(int isClear) throws RemoteException {
            students.clear();
        }
    };
}

你可能感兴趣的:(AIDL中的in out inout)