Book.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43package com.honszeal.processdemo;
import android.os.Parcel;
import android.os.Parcelable;
/**
* date:2018/3/13 on 16:07
* desc:
* author: liuch
*/
public class Book implements Parcelable{
private int bookId;
private String bookName;
protected 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);
}
}
IDE 自动完成
Book.aidl1
2
3package com.honszeal.processdemo;
parcelable Book;
ILibraryManager.aidl1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16package com.honszeal.processdemo;
import com.honszeal.processdemo.Book;
interface ILibraryManager {
List<Book> getNewBookList();
void donateBook(in Book book);
int add(int a, int b);
void log(String message);
}
编译一下
切换到 project
选项卡,在 app -> build -> generated -> source -> aidl -> debug -> 包名
下 找到系统生成的ILibraryManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121package com.honszeal.processdemo;
public interface ILibraryManager extends android.os.IInterface {
public static abstract class Stub extends android.os.Binder implements com.honszeal.processdemo.ILibraryManager {
private static final java.lang.String DESCRIPTOR = "com.honszeal.processdemo.ILibraryManager";
public Stub() {
this.attachInterface(this, DESCRIPTOR);
}
public static com.honszeal.processdemo.ILibraryManager asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof com.honszeal.processdemo.ILibraryManager))) {
return ((com.honszeal.processdemo.ILibraryManager) iin);
}
return new com.honszeal.processdemo.ILibraryManager.Stub.Proxy(obj);
}
@Override
public android.os.IBinder asBinder() {
return this;
}
@Override
public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_getNewBookList: {
data.enforceInterface(DESCRIPTOR);
java.util.List<com.honszeal.processdemo.Book> _result = this.getNewBookList();
reply.writeNoException();
reply.writeTypedList(_result);
return true;
}
case TRANSACTION_donateBook: {
data.enforceInterface(DESCRIPTOR);
com.honszeal.processdemo.Book _arg0;
if ((0 != data.readInt())) {
_arg0 = com.honszeal.processdemo.Book.CREATOR.createFromParcel(data);
} else {
_arg0 = null;
}
this.donateBook(_arg0);
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.honszeal.processdemo.ILibraryManager {
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote) {
mRemote = remote;
}
@Override
public android.os.IBinder asBinder() {
return mRemote;
}
public java.lang.String getInterfaceDescriptor() {
return DESCRIPTOR;
}
@Override
public java.util.List<com.honszeal.processdemo.Book> getNewBookList() throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.util.List<com.honszeal.processdemo.Book> _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getNewBookList, _data, _reply, 0);
_reply.readException();
_result = _reply.createTypedArrayList(com.honszeal.processdemo.Book.CREATOR);
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override
public void donateBook(com.honszeal.processdemo.Book book) throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
if ((book != null)) {
_data.writeInt(1);
book.writeToParcel(_data, 0);
} else {
_data.writeInt(0);
}
mRemote.transact(Stub.TRANSACTION_donateBook, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_getNewBookList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_donateBook = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
public java.util.List<com.honszeal.processdemo.Book> getNewBookList() throws android.os.RemoteException;
public void donateBook(com.honszeal.processdemo.Book book) throws android.os.RemoteException;
}
BookService.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24public class BookService extends Service {
private Binder binder = new ILibraryManager.Stub() {
@Override
public List<Book> getNewBookList() throws RemoteException {
return null;
}
@Override
public void donateBook(Book book) throws RemoteException {
}
};
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
}
手写
IBookManager.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17package com.honszeal.processdemo;
import android.os.IInterface;
import android.os.RemoteException;
import java.util.List;
public interface IBookManager extends IInterface {
String DESCRIPTOR = "com.honszeal.processdemo.ILibraryManager";
int TRANSACTION_getNewBookList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
int TRANSACTION_donateBook = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
List<Book> getNewBookList() throws RemoteException;
void donateBook(Book book) throws RemoteException;
}
BookManagerImpl1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108package com.honszeal.processdemo;
import android.os.Binder;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Parcel;
import android.os.RemoteException;
import java.util.List;
public class BookManagerImpl extends Binder implements IBookManager {
public BookManagerImpl() {
this.attachInterface(this, DESCRIPTOR);
}
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);
}
@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_getNewBookList:
data.enforceInterface(DESCRIPTOR);
List<Book> bookList = this.getNewBookList();
reply.writeNoException();
reply.writeTypedList(bookList);
return true;
case TRANSACTION_donateBook:
data.enforceInterface(DESCRIPTOR);
Book arg0;
if (0 != data.readInt()) {
arg0 = Book.CREATOR.createFromParcel(data);
} else {
arg0 = null;
}
this.donateBook(arg0);
reply.writeNoException();
return true;
}
return super.onTransact(code, data, reply, flags);
}
@Override
public List<Book> getNewBookList() throws RemoteException {
return null;
}
@Override
public void donateBook(Book book) throws RemoteException {
}
@Override
public IBinder asBinder() {
return this;
}
private static class Proxy implements IBookManager {
private IBinder mRemote;
public Proxy(IBinder mRemote) {
this.mRemote = mRemote;
}
public String getInterfaceDescriptor() {
return DESCRIPTOR;
}
@Override
public List<Book> getNewBookList() throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
List<Book> result;
data.writeInterfaceToken(getInterfaceDescriptor());
mRemote.transact(TRANSACTION_getNewBookList, data, reply, 0);
reply.readException();
result = reply.createTypedArrayList(Book.CREATOR);
return result;
}
@Override
public void donateBook(Book book) throws RemoteException {
//
}
@Override
public IBinder asBinder() {
return mRemote;
}
}
}
感觉,就是具体的实现在服务端写,在客户端调用。实际上就是调用了 Proxy
的对应方法,然后 Binder
的 transact
方法 再就是 onTransact
方法。
权限验证
- 在
onBind
中进行验证,验证不通过就直接返回null
, 这样验证失败的客户端直接无法绑定服务。 - 在
onTransact
中进行验证,验证不通过就直接返回false
。