Binder의 구현
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);
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.