1、Activity中:
private Window mWindow = null;
mWindow = getWindow();
Log.d(tag, "mWindow.getClass().getName() is: " + mWindow.getClass().getName());
D/MainActivity( 437): mWindow.getClass().getName() is: com.android.internal.policy.impl.PhoneWindow
2、Activity中:
private WindowManager mWindowManager = null;
mWindowManager = getWindowManager();
Log.d(tag, "mWindowManager.getClass().getName() is: " + mWindowManager.getClass().getName());
D/MainActivity( 437): mWindowManager.getClass().getName() is: android.view.Window$LocalWindowManager
3、ServiceManager(java)中:
private IServiceManager mISM = null;
mISM = ServiceManager.getIServiceManager();
Log.d(tag, "mISM.toString() is: " + mISM.toString());
Log.d(tag, "mISM.getClass().getName() is: " + mISM.getClass().getName());
D/ServiceManager002Activity( 712): mISM.toString() is: android.os.ServiceManagerProxy@40f169f0
D/ServiceManager002Activity( 712): mISM.getClass().getName() is: android.os.ServiceManagerProxy
4、Context.ACTIVITY_SERVICE
IBinder activity = ServiceManager.getService(Context.ACTIVITY_SERVICE);
Log.d(tag, "activity.toString() is: " + activity.toString());
Log.d(tag, "activity.getClass().getName() is: " + activity.getClass().getName());
D/ServiceManager002Activity( 712): activity.toString() is: android.os.BinderProxy@40f16a80
D/ServiceManager002Activity( 712): activity.getClass().getName() is: android.os.BinderProxy
5、Context.ALARM_SERVICE
IBinder alarm = ServiceManager.getService(Context.ALARM_SERVICE);
Log.d(tag, "alarm.toString() is: " + alarm.toString());
Log.d(tag, "alarm.getClass().getName() is: " + alarm.getClass().getName());
D/ServiceManager002Activity( 712): alarm.toString() is: android.os.BinderProxy@40f18228
D/ServiceManager002Activity( 712): alarm.getClass().getName() is: android.os.BinderProxy
6、Context.INPUT_METHOD_SERVICE
IBinder input_method = ServiceManager.getService(Context.INPUT_METHOD_SERVICE);
Log.d(tag, "input_method.toString() is: " + input_method.toString());
Log.d(tag, "input_method.getClass().getName() is: " + input_method.getClass().getName());
D/ServiceManager002Activity( 712): input_method.toString() is: android.os.BinderProxy@40f4a7b0
D/ServiceManager002Activity( 712): input_method.getClass().getName() is: android.os.BinderProxy
7、Context.POWER_SERVICE
IBinder power = ServiceManager.getService(Context.POWER_SERVICE);
Log.d(tag, "power.toString() is: " + power.toString());
Log.d(tag, "power.getClass().getName() is: " + power.getClass().getName());
D/ServiceManager002Activity( 712): power.toString() is: android.os.BinderProxy@40f34c38
D/ServiceManager002Activity( 712): power.getClass().getName() is: android.os.BinderProxy
8、Context.WINDOW_SERVICE
IBinder window = ServiceManager.getService(Context.WINDOW_SERVICE);
Log.d(tag, "window.toString() is: " + window.toString());
Log.d(tag, "window.getClass().getName() is: " + window.getClass().getName());
D/ServiceManager002Activity( 712): window.toString() is: android.os.BinderProxy@40f180a0
D/ServiceManager002Activity( 712): window.getClass().getName() is: android.os.BinderProxy
ServiceManager.getService()函数的源码为如下:
public static IBinder getService(String name)
{
try
{
IBinder service = sCache.get(name);
if (service != null)
{
return service;
}
else
{
return getIServiceManager().getService(name);
}
}
catch (RemoteException e)
{
Log.e(TAG, "error in getService", e);
}
return null;
}
该函数中又调用:getIServiceManager().getService(name);
private static IServiceManager getIServiceManager()
{
if (sServiceManager != null)
{
return sServiceManager;
}
// Find the service manager
sServiceManager = ServiceManagerNative.asInterface(BinderInternal.getContextObject());
return sServiceManager;
}
通过Point 3可以知道getIServiceManager的返回值是 android.os.ServiceManagerProxy类型的,
调用 android.os.ServiceManagerProxy的getService函数:
public IBinder getService(String name) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
data.writeInterfaceToken(IServiceManager.descriptor);
data.writeString(name);
mRemote.transact(GET_SERVICE_TRANSACTION, data, reply, 0);
IBinder binder = reply.readStrongBinder();
reply.recycle();
data.recycle();
return binder;
}
该代理会与ServiceManager的Server部分进行通信。
9、BinderInternal.getContextObject()
private IBinder mIBinder = null;
mIBinder = BinderInternal.getContextObject();
Log.d(tag, "mIBinder.toString() is: " + mIBinder.toString());
Log.d(tag, "mIBinder.getClass().getName() is: " + mIBinder.getClass().getName());
D/ActivityManagerNative002Activity( 743): mIBinder.toString() is: android.os.BinderProxy@40f17310
D/ActivityManagerNative002Activity( 743): mIBinder.getClass().getName() is: android.os.BinderProxy
10、ServiceManagerProxy的成员变量:mRemote
Log.d(tag, "remote.toString() is: " + remote.toString());
Log.d(tag, "remote.getClass().getName() is: " + remote.getClass().getName());
D/ServiceManager002Activity( 730): remote.toString() is: android.os.BinderProxy@40f14c80
D/ServiceManager002Activity( 730): remote.getClass().getName() is: android.os.BinderProxy
mRemote的值与ServiceManager中调用BinderInternal.getContextObject()的返回值是一致的:
ServiceManager中:ServiceManagerNative.asInterface(BinderInternal.getContextObject())
ServiceManagerNative中:new ServiceManagerProxy(obj) obj是BinderInternal.getContextObject()的返回值
ServiceManagerProxy中:mRemote = remote remote是ServiceManagerProxy构造函数的参数
因此,mRemote实际就是BinderInternal.getContextObject()的返回值