java层添加IBinder服务;
上层应用调用ServiceManager.java的流程::
frameworks/base/core/java/android/os/ServiceManager.java
以为addService为主线:::
public static void addService(String name, IBinder service) {
addService(name, service, false, IServiceManager.DUMP_FLAG_PRIORITY_DEFAULT);
}
public static void addService(String name, IBinder service, boolean allowIsolated) {
addService(name, service, allowIsolated, IServiceManager.DUMP_FLAG_PRIORITY_DEFAULT);
}
public static void addService(String name, IBinder service, boolean allowIsolated,
int dumpPriority) {
try {
getIServiceManager().addService(name, service, allowIsolated, dumpPriority);
} catch (RemoteException e) {
Log.e(TAG, "error in addService", e);
}
}
private static IServiceManager sServiceManager;
private static IServiceManager getIServiceManager() {
if (sServiceManager != null) {
return sServiceManager;
}
// Find the service manager
sServiceManager = ServiceManagerNative
.asInterface(Binder.allowBlocking(BinderInternal.getContextObject()));
return sServiceManager;
}
调用ServiceManagerNative.java中的asInterface()方法,首先需要获取IBinder对象,然后检测是否可以被阻塞:
BinderInternal.getContextObject()的代码如下,是一个本地方法;
frameworks/base/core/java/com/android/internal/os/BinderInternal.java
/**
* Return the global "context object" of the system. This is usually
* an implementation of IServiceManager, which you can use to find
* other services.
*/
public static final native IBinder getContextObject();
在android_util_Binder.cpp文件中,是一个静态的方法;
frameworks/base/core/jni/android_util_Binder.cpp
static jobject android_os_BinderInternal_getContextObject(JNIEnv* env, jobject clazz)
{
sp<IBinder> b = ProcessState::self()->getContextObject(NULL);
return javaObjectForIBinder(env, b);
}
调用ProcessState的getContextObject()方法:
sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
{
return getStrongProxyForHandle(0);
}
调用参数为0的 getStrongProxyForHandle 函数,获取IBinder的上下文管理对象:
上面已经分析过函数getStrongProxyForHandle的功能就是获取到一个远程的IBinder上下文管理对象
然后返回IBinder的对象;拿到了servicemanager的管理对象,就好处理了,首先判断是否为可阻塞的IBinder对象,
然后调用ServiceManagerNative.asInterface方法,实际上返回的ServiceManagerProxy的代理类对象:
frameworks/base/core/java/android/os/ServiceManagerNative.java
public abstract class ServiceManagerNative extends Binder implements IServiceManager
{
/**
* Cast a Binder object into a service manager interface, generating
* a proxy if needed.
*/
static public IServiceManager asInterface(IBinder obj)
{
if (obj == null) {
return null;
}//根据obj查询descriptor的管理类对象;,这个obj是c++层的对象哦,
IServiceManager in =
(IServiceManager)obj.queryLocalInterface(descriptor);
if (in != null) {
return in;
}
return new ServiceManagerProxy(obj);
}
......
public ServiceManagerNative()
{
attachInterface(this, descriptor);
}
public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
{
try {
switch (code) {
......
case IServiceManager.ADD_SERVICE_TRANSACTION: {
data.enforceInterface(IServiceManager.descriptor);
String name = data.readString();
IBinder service = data.readStrongBinder();
boolean allowIsolated = data.readInt() != 0;
int dumpPriority = data.readInt();
addService(name, service, allowIsolated, dumpPriority);
return true;
}
......
}
} catch (RemoteException e) {
}
return false;
}
public IBinder asBinder()
{
return this;
}
}
class ServiceManagerProxy implements IServiceManager {
public ServiceManagerProxy(IBinder remote) {
mRemote = remote;
}
public IBinder asBinder() {
return mRemote;
}
public void addService(String name, IBinder service, boolean allowIsolated, int dumpPriority)
throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
data.writeInterfaceToken(IServiceManager.descriptor);
data.writeString(name);
data.writeStrongBinder(service);
data.writeInt(allowIsolated ? 1 : 0);
data.writeInt(dumpPriority);
if (SystemProperties.get("persist.support.securetest").equals("1")) {
try{
mRemote.transact(ADD_SERVICE_TRANSACTION, data, reply, 0);
}
catch (SecurityException e) {
}
}
else {
mRemote.transact(ADD_SERVICE_TRANSACTION, data, reply, 0);
}
reply.recycle();
data.recycle();
}
......
}
在onTraction中和servicemanager进行Binder通信,,如添加SerialService服务
frameworks/base/services/java/com/android/server/SystemServer.java
try {
// Serial port support
serial = new SerialService(context);
ServiceManager.addService(Context.SERIAL_SERVICE, serial);
} catch (Throwable e) {
Slog.e(TAG, "Failure starting SerialService", e);
}
至此,所有service_manager的讲述就到此为止;