https://www.cnblogs.com/winfu/p/7521372.html
IHelloService.aidl:
/** {@hide} */
interface IHelloService
{
void sayhello();
int sayhello_to(String name);
}
1.把 IHelloService.aidl 放入 frameworks/base/core/java/android/os
2.修改 frameworks/base/Android.mk 添加一行
core/java/android/os/IVibratorService.aidl \
+ core/java/android/os/IHelloService.aidl \
3. 编译/framework/base
4.系统自动生成IHelloService.java
public interface IHelloService extends android.os.IInterface
{
public static abstract class Stub extends android.os.Binder implements IHelloService{
private static final java.lang.String DESCRIPTOR = "IHelloService";
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
public static IHelloService asInterface(android.os.IBinder obj)
{ ... ...
return new IHelloService.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) ...
{
switch (code)
{
case INTERFACE_TRANSACTION:
case TRANSACTION_sayhello:
case TRANSACTION_sayhello_to:
}
}
private static class Proxy implements IHelloService
static final int TRANSACTION_sayhello = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_sayhello_to = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
public void sayhello() throws android.os.RemoteException;
public int sayhello_to(java.lang.String name) throws android.os.RemoteException;
}
IHelloService.java 的结构为:
1.名字为IHelloService 的interface:
2.IHelloService 这个interface 中有一个继承Binder 的抽象类stub
IHelloService.aidl 中已经定义好的service 中的几个函数的虚函数
public interface IHelloService extends android.os.IInterface
{
public static abstract class Stub extends android.os.Binder implements IHelloService ...
public void sayhello() throws android.os.RemoteException;
public int sayhello_to(java.lang.String name) throws android.os.RemoteException;
}
import android.util.Slog;
public class HelloService extends IHelloService.Stub {
private static final String TAG = "HelloService";
... ...
public void sayhello() throws android.os.RemoteException {
... ...
}
public int sayhello_to(java.lang.String name) throws android.os.RemoteException {
... ...
}
}
1. HelloService.java 继承 IHelloService.Stub 这个内部类;
2. HelloService.java 中复写 之前在IHelloService.aidl 中定义好的函数
import android.util.Slog;
import android.os.ServiceManager;
import android.os.IBinder;
public class TestClient {
public static void main(String args[])
{
if (args[0].equals("hello"))
{
/* 1. getService */
IBinder binder = ServiceManager.getService("hello");
IHelloService svr = IHelloService.Stub.asInterface(binder);
svr.sayhello();
int cnt = svr.sayhello_to(args[1]);
}
}
}
=========================================================================================
https://www.jianshu.com/p/adaa1a39a274
Parcel是一个容器,主要就是用来进行IPC通信的。
Parcel parcle = Parcel.Obtain();
或者
new Parcel();
parcel.writeInt(int val); // 传入Int 型数据
parcel.writeString(String val); //传入String 型 数据
private PrintJobInfo(@NonNull Parcel parcel) {
.. .. ..
mLabel = parcel.readString();
mState = parcel.readInt();
.. .. ..
struct binder_proc {
struct hlist_node proc_node;
struct rb_root threads;
struct rb_root nodes;
struct rb_root refs_by_desc;
struct rb_root refs_by_node;
... ...
struct list_head todo;
wait_queue_head_t wait;
... ...
};
nodes : Binder实体在内核中对应的数据结构
binder_node : 记录进程相关的binder_proc