这里要用到的Binder进程间通信接口定义在src/shy/luo/ashmem/IMemoryService.java文件中:
- package shy.luo.ashmem;
-
- import android.util.Log;
- import android.os.IInterface;
- import android.os.Binder;
- import android.os.IBinder;
- import android.os.Parcel;
- import android.os.ParcelFileDescriptor;
- import android.os.RemoteException;
-
- public interface IMemoryService extends IInterface {
- public static abstract class Stub extends Binder implements IMemoryService {
- private static final String DESCRIPTOR = "shy.luo.ashmem.IMemoryService";
-
- public Stub() {
- attachInterface(this, DESCRIPTOR);
- }
-
- public static IMemoryService asInterface(IBinder obj) {
- if (obj == null) {
- return null;
- }
-
- IInterface iin = (IInterface)obj.queryLocalInterface(DESCRIPTOR);
- if (iin != null && iin instanceof IMemoryService) {
- return (IMemoryService)iin;
- }
-
- return new IMemoryService.Stub.Proxy(obj);
- }
-
- public IBinder asBinder() {
- return this;
- }
-
- @Override
- public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws android.os.RemoteException {
- switch (code) {
- case INTERFACE_TRANSACTION: {
- reply.writeString(DESCRIPTOR);
- return true;
- }
- case TRANSACTION_getFileDescriptor: {
- data.enforceInterface(DESCRIPTOR);
-
- ParcelFileDescriptor result = this.getFileDescriptor();
-
- reply.writeNoException();
-
- if (result != null) {
- reply.writeInt(1);
- result.writeToParcel(reply, 0);
- } else {
- reply.writeInt(0);
- }
-
- return true;
- }
- case TRANSACTION_setValue: {
- data.enforceInterface(DESCRIPTOR);
-
- int val = data.readInt();
- setValue(val);
-
- reply.writeNoException();
-
- return true;
- }
- }
-
- return super.onTransact(code, data, reply, flags);
- }
-
- private static class Proxy implements IMemoryService {
- private IBinder mRemote;
-
- Proxy(IBinder remote) {
- mRemote = remote;
- }
-
- public IBinder asBinder() {
- return mRemote;
- }
-
- public String getInterfaceDescriptor() {
- return DESCRIPTOR;
- }
-
- public ParcelFileDescriptor getFileDescriptor() throws RemoteException {
- Parcel data = Parcel.obtain();
- Parcel reply = Parcel.obtain();
-
- ParcelFileDescriptor result;
-
- try {
- data.writeInterfaceToken(DESCRIPTOR);
-
- mRemote.transact(Stub.TRANSACTION_getFileDescriptor, data, reply, 0);
-
- reply.readException();
- if (0 != reply.readInt()) {
- result = ParcelFileDescriptor.CREATOR.createFromParcel(reply);
- } else {
- result = null;
- }
- } finally {
- reply.recycle();
- data.recycle();
- }
-
- return result;
- }
-
- public void setValue(int val) throws RemoteException {
- Parcel data = Parcel.obtain();
- Parcel reply = Parcel.obtain();
-
- try {
- data.writeInterfaceToken(DESCRIPTOR);
- data.writeInt(val);
-
- mRemote.transact(Stub.TRANSACTION_setValue, data, reply, 0);
-
- reply.readException();
- } finally {
- reply.recycle();
- data.recycle();
- }
- }
- }
-
- static final int TRANSACTION_getFileDescriptor = IBinder.FIRST_CALL_TRANSACTION + 0;
- static final int TRANSACTION_setValue = IBinder.FIRST_CALL_TRANSACTION + 1;
-
- }
-
- public ParcelFileDescriptor getFileDescriptor() throws RemoteException;
- public void setValue(int val) throws RemoteException;
- }
- package shy.luo.ashmem;
-
- import android.util.Log;
- import android.os.IInterface;
- import android.os.Binder;
- import android.os.IBinder;
- import android.os.Parcel;
- import android.os.ParcelFileDescriptor;
- import android.os.RemoteException;
-
- public interface IMemoryService extends IInterface {
- public static abstract class Stub extends Binder implements IMemoryService {
- private static final String DESCRIPTOR = "shy.luo.ashmem.IMemoryService";
-
- public Stub() {
- attachInterface(this, DESCRIPTOR);
- }
-
- public static IMemoryService asInterface(IBinder obj) {
- if (obj == null) {
- return null;
- }
-
- IInterface iin = (IInterface)obj.queryLocalInterface(DESCRIPTOR);
- if (iin != null && iin instanceof IMemoryService) {
- return (IMemoryService)iin;
- }
-
- return new IMemoryService.Stub.Proxy(obj);
- }
-
- public IBinder asBinder() {
- return this;
- }
-
- @Override
- public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws android.os.RemoteException {
- switch (code) {
- case INTERFACE_TRANSACTION: {
- reply.writeString(DESCRIPTOR);
- return true;
- }
- case TRANSACTION_getFileDescriptor: {
- data.enforceInterface(DESCRIPTOR);
-
- ParcelFileDescriptor result = this.getFileDescriptor();
-
- reply.writeNoException();
-
- if (result != null) {
- reply.writeInt(1);
- result.writeToParcel(reply, 0);
- } else {
- reply.writeInt(0);
- }
-
- return true;
- }
- case TRANSACTION_setValue: {
- data.enforceInterface(DESCRIPTOR);
-
- int val = data.readInt();
- setValue(val);
-
- reply.writeNoException();
-
- return true;
- }
- }
-
- return super.onTransact(code, data, reply, flags);
- }
-
- private static class Proxy implements IMemoryService {
- private IBinder mRemote;
-
- Proxy(IBinder remote) {
- mRemote = remote;
- }
-
- public IBinder asBinder() {
- return mRemote;
- }
-
- public String getInterfaceDescriptor() {
- return DESCRIPTOR;
- }
-
- public ParcelFileDescriptor getFileDescriptor() throws RemoteException {
- Parcel data = Parcel.obtain();
- Parcel reply = Parcel.obtain();
-
- ParcelFileDescriptor result;
-
- try {
- data.writeInterfaceToken(DESCRIPTOR);
-
- mRemote.transact(Stub.TRANSACTION_getFileDescriptor, data, reply, 0);
-
- reply.readException();
- if (0 != reply.readInt()) {
- result = ParcelFileDescriptor.CREATOR.createFromParcel(reply);
- } else {
- result = null;
- }
- } finally {
- reply.recycle();
- data.recycle();
- }
-
- return result;
- }
-
- public void setValue(int val) throws RemoteException {
- Parcel data = Parcel.obtain();
- Parcel reply = Parcel.obtain();
-
- try {
- data.writeInterfaceToken(DESCRIPTOR);
- data.writeInt(val);
-
- mRemote.transact(Stub.TRANSACTION_setValue, data, reply, 0);
-
- reply.readException();
- } finally {
- reply.recycle();
- data.recycle();
- }
- }
- }
-
- static final int TRANSACTION_getFileDescriptor = IBinder.FIRST_CALL_TRANSACTION + 0;
- static final int TRANSACTION_setValue = IBinder.FIRST_CALL_TRANSACTION + 1;
-
- }
-
- public ParcelFileDescriptor getFileDescriptor() throws RemoteException;
- public void setValue(int val) throws RemoteException;
- }
package shy.luo.ashmem;
import android.util.Log;
import android.os.IInterface;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
public interface IMemoryService extends IInterface {
public static abstract class Stub extends Binder implements IMemoryService {
private static final String DESCRIPTOR = "shy.luo.ashmem.IMemoryService";
public Stub() {
attachInterface(this, DESCRIPTOR);
}
public static IMemoryService asInterface(IBinder obj) {
if (obj == null) {
return null;
}
IInterface iin = (IInterface)obj.queryLocalInterface(DESCRIPTOR);
if (iin != null && iin instanceof IMemoryService) {
return (IMemoryService)iin;
}
return new IMemoryService.Stub.Proxy(obj);
}
public IBinder asBinder() {
return this;
}
@Override
public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws android.os.RemoteException {
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_getFileDescriptor: {
data.enforceInterface(DESCRIPTOR);
ParcelFileDescriptor result = this.getFileDescriptor();
reply.writeNoException();
if (result != null) {
reply.writeInt(1);
result.writeToParcel(reply, 0);
} else {
reply.writeInt(0);
}
return true;
}
case TRANSACTION_setValue: {
data.enforceInterface(DESCRIPTOR);
int val = data.readInt();
setValue(val);
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements IMemoryService {
private IBinder mRemote;
Proxy(IBinder remote) {
mRemote = remote;
}
public IBinder asBinder() {
return mRemote;
}
public String getInterfaceDescriptor() {
return DESCRIPTOR;
}
public ParcelFileDescriptor getFileDescriptor() throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
ParcelFileDescriptor result;
try {
data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getFileDescriptor, data, reply, 0);
reply.readException();
if (0 != reply.readInt()) {
result = ParcelFileDescriptor.CREATOR.createFromParcel(reply);
} else {
result = null;
}
} finally {
reply.recycle();
data.recycle();
}
return result;
}
public void setValue(int val) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
try {
data.writeInterfaceToken(DESCRIPTOR);
data.writeInt(val);
mRemote.transact(Stub.TRANSACTION_setValue, data, reply, 0);
reply.readException();
} finally {
reply.recycle();
data.recycle();
}
}
}
static final int TRANSACTION_getFileDescriptor = IBinder.FIRST_CALL_TRANSACTION + 0;
static final int TRANSACTION_setValue = IBinder.FIRST_CALL_TRANSACTION + 1;
}
public ParcelFileDescriptor getFileDescriptor() throws RemoteException;
public void setValue(int val) throws RemoteException;
} 这里主要是定义了IMemoryService接口,它里面有两个调用接口:
- public ParcelFileDescriptor getFileDescriptor() throws RemoteException;
- public void setValue(int val) throws RemoteException;
- public ParcelFileDescriptor getFileDescriptor() throws RemoteException;
- public void setValue(int val) throws RemoteException;
public ParcelFileDescriptor getFileDescriptor() throws RemoteException;
public void setValue(int val) throws RemoteException;
同时,还分别定义了用于Server端实现的IMemoryService.Stub基类和用于Client端使用的代理IMemoryService.Stub.Proxy类。关于Binder进程间通信机制在应用程序框架层的Java接口定义,请参考前面
Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析一文。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/13164110/viewspace-747972/
,如需转载,请注明出处,否则将追究法律责任。