Binder의 구현

7450 단어
1. AIDL
    1. Book.java Parcelable 인터페이스 구현
package com.android.shieh.processtest.aidl;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by GKX100187 on 2015/11/27.
 */
public class Book implements Parcelable {

    public int bookId;
    public String bookName;

    public Book(int bookId, String bookName){
        this.bookId = bookId;
        this.bookName = bookName;
    }

    private Book(Parcel in) {
        bookId = in.readInt();
        bookName = in.readString();
    }

    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 dest, int flags) {
        dest.writeInt(bookId);
        dest.writeString(bookName);
    }
}

    
    2. Book.AIDL에서 Book 클래스 선언
package com.android.shieh.processtest.aidl;
parcelable Book;//  Book 

    
    3.BookManager.aidl 정의 인터페이스
// IBookManager.aidl
package com.android.shieh.processtest.aidl;

// Declare any non-default types here with import statements
import com.android.shieh.processtest.aidl.Book;//        import  
interface IBookManager {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    List<Book> getBookList();
    void addBook(in Book book);
}

IDE는 인터페이스에 따라 자동으로 해당 인터페이스를 생성합니다.java 클래스, 수동으로 생성된 BookManagerImpl 클래스와 유사
2. 수동으로 생성 1.AIDL 성격의 인터페이스는 IBookManager를 선언합니다.java
package com.android.shieh.processtest.manualbinder;

import android.os.IBinder;
import android.os.IInterface;
import android.os.RemoteException;

import com.android.shieh.processtest.aidl.Book;

import java.util.List;

/**
 * Created by GKX100187 on 2015/11/30.
 */
public interface IBookManager extends IInterface {
    static final String DESCRIPTOR //Binder      ,     Binder    
            = "com.android.shieh.processtest.manualbinder.IBookManager";
    static final int TRANSACTION_getBookList = IBinder//     id
            .FIRST_CALL_TRANSACTION + 0;
    static final int TRANSACTION_addBook = IBinder
            .FIRST_CALL_TRANSACTION + 1;

    public List<Book> getBookList() throws RemoteException;

    public void addBook(Book book) throws RemoteException;
}

    
    2.Stub 클래스 및 Stub 클래스의 프록시 클래스 구현
package com.android.shieh.processtest.manualbinder;

import android.os.Binder;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Parcel;
import android.os.RemoteException;

import com.android.shieh.processtest.aidl.Book;

import java.util.List;

/**
 * Created by GKX100187 on 2015/11/30.
 */
public class BookManagerImpl extends Binder implements IBookManager{

    public BookManagerImpl() {
        //    
        this.attachInterface(this,DESCRIPTOR);
    }

    /**       Binder           AIDL       
     *             ,       Stub  
     *             Stub.proxy(  Stub,      )
     */
     public static IBookManager asInterface(IBinder obj){
        if(obj == null){
            return null;
        }
        IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
        if(((iin != null) && (iin instanceof IBookManager))){
            return ((IBookManager)iin);
        }
        return new BookManagerImpl.Proxy(obj);
    }

    /**
     *      Binder  
     * */
    @Override
    public IBinder asBinder() {
        return this;
    }

    /**
     *          Binder   ,
     *      code           ,
     * data       
     * reply     
     *   false           
     * */
    @Override
    protected boolean onTransact(int code, Parcel data, Parcel reply, int flags)
            throws RemoteException {
        switch (code){
            case INTERFACE_TRANSACTION:
                reply.writeString(DESCRIPTOR);
                return true;
            case TRANSACTION_getBookList:
                data.enforceInterface(DESCRIPTOR);
                List<Book> result = this.getBookList();
                reply.writeNoException();
                reply.writeTypedList(result);
                return true;
            case TRANSACTION_addBook:
                data.enforceInterface(DESCRIPTOR);
                Book arg0;
                if(data.readInt()!=0){
                    arg0 = Book.CREATOR.createFromParcel(data);
                }else {
                    arg0 = null;
                }
                reply.writeNoException();
                return true;
        }
        return super.onTransact(code, data, reply, flags);
    }

    @Override
    public List<Book> getBookList() throws RemoteException {
        return null;
    }

    @Override
    public void addBook(Book book) throws RemoteException {

    }

    private static class Proxy implements IBookManager{

        private IBinder mRemote;

        Proxy(IBinder remote){
            mRemote = remote;
        }

        @Override
        public IBinder asBinder() {
            return mRemote;
        }

        public String getInterfaceDescriptor() {
            return DESCRIPTOR;
        }

        /**
         *       
         * */
        @Override
        public List<Book> getBookList() throws RemoteException {

            Parcel data = Parcel.obtain();
            Parcel reply = Parcel.obtain();

            List<Book> result;

            try {
                data.writeInterfaceToken(DESCRIPTOR);
                //        ,    ,      onTransact()       
                mRemote.transact(TRANSACTION_getBookList,data,reply,0);
                //                
                reply.readException();//        
                result = reply.createTypedArrayList(Book.CREATOR);
            }finally {
                reply.recycle();
                data.recycle();
            }
            return result;
        }

        @Override
        public void addBook(Book book) throws RemoteException {

            Parcel data = Parcel.obtain();
            Parcel reply = Parcel.obtain();

            try{
                data.writeInterfaceToken(DESCRIPTOR);
                if((book != null)) {
                    data.writeInt(1);
                    book.writeToParcel(data, 0);
                } else {
                    data.writeInt(0);
                }
                mRemote.transact(TRANSACTION_addBook,data,reply,0);
                reply.readException();
            }finally {
                reply.recycle();
                data.recycle();
            }
        }
        
    }

}

3. Binder의 사망 에이전트 DethRecipient
private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient(){

    @Override
    public void binderDied() {
        if(mBookManager == null){
            return ;
        }
        mBookManager.asBinder().unlinkToDeath(mDeathRecipient,0);
        mBookManager = null;
        //       Service
        
    }
};

클라이언트 바인딩 원격 서비스, Binder에 데스 에이전트 설정
mService = IMessageBoxManager.Stub.asInterface(binder);
binder.linkToDeath(mDeathRecipient);

좋은 웹페이지 즐겨찾기