源码位置
android.app.ActivityManagerNative.java
public abstract class ActivityManagerNative extends Binder
implements IActivityManager{}
ActivityManagerNative是一个抽象类,继承了Binder类,实现了IActivityManager接口
ActivityManagerService是它的子类,也就实现了IActivityManager接口,
下面的ActivityManagerProxy类也实现了IActivityManager接口
getDefault()方法, 最终得到的是一个ActivityManagerProxy对象
static public IActivityManager getDefault() {
return gDefault.get();
}
查看gDefault
private static final Singleton<IActivityManager> gDefault = new Singleton<IActivityManager>() {
protected IActivityManager create() {
//获取到ActivityManagerService的Binder
IBinder b = ServiceManager.getService("activity");
if (false) {
Log.v("ActivityManager", "default service binder = " + b);
}
//得到一个ActivityManagerProxy对象
IActivityManager am = asInterface(b);
if (false) {
Log.v("ActivityManager", "default service = " + am);
}
return am;
}
};
查看asInterface方法,返回了一个ActivityManagerProxy对象
/**
* Cast a Binder object into an activity manager interface, generating
* a proxy if needed.
* 把一个Binder对象强转为一个activity manger 接口,生成一个代理对象
*/
static public IActivityManager asInterface(IBinder obj) {
if (obj == null) {
return null;
}
//String descriptor = "android.app.IActivityManager";
IActivityManager in =
(IActivityManager)obj.queryLocalInterface(descriptor);
if (in != null) {
return in;
}
//返回了一个ActivityManagerProxy
return new ActivityManagerProxy(obj);
}
查看queryLocalInterface方法
/**
* Attempt to retrieve a local implementation of an interface
* for this Binder object. If null is returned, you will need
* to instantiate a proxy class to marshall calls through
* the transact() method.
* 尝试为这个binder对象取出一个本地接口实现。如果返回null,你需要通过transaction()实例化一个代理对象
* 引领调用命令
*
*/
public IInterface queryLocalInterface(String descriptor);
android.os.Binder.java,实现了IBinder接口
/**
* Use information supplied to attachInterface() to return the
* associated IInterface if it matches the requested
* descriptor.
*/
public IInterface queryLocalInterface(String descriptor) {
if (mDescriptor.equals(descriptor)) {
return mOwner;
}
return null;
}
查看attachInterface()方法
/**
* Convenience method for associating a specific interface with the Binder.
* After calling, queryLocalInterface() will be implemented for you
* to return the given owner IInterface when the corresponding
* descriptor is requested.
这个方法把一个特定的接口和Binder关联在一起。调用这个方法之后,queryLocalInterface() 会被实现,然后当相关的descriptor
被请求的时候,你就可以把owner IInterface返回给他
*/
public void attachInterface(IInterface owner, String descriptor) {
mOwner = owner;
mDescriptor = descriptor;
}
最后查看ActivityManagerProxy类,这是ActivityManagerNative的内部类。它实现了IActivityManager接口,
通过Binder通信,最终会调用ActivityManagerService里面的类似startActivity等等的方法
class ActivityManagerProxy implements IActivityManager
{
public ActivityManagerProxy(IBinder remote)
{
mRemote = remote;
}
public IBinder asBinder()
{
return mRemote;
}
public int startActivity(IApplicationThread caller, String callingPackage, Intent intent,
String resolvedType, IBinder resultTo, String resultWho, int requestCode,
int startFlags, ProfilerInfo profilerInfo, Bundle options) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
data.writeInterfaceToken(IActivityManager.descriptor);
data.writeStrongBinder(caller != null ? caller.asBinder() : null);
data.writeString(callingPackage);
intent.writeToParcel(data, 0);
data.writeString(resolvedType);
data.writeStrongBinder(resultTo);
data.writeString(resultWho);
data.writeInt(requestCode);
data.writeInt(startFlags);
if (profilerInfo != null) {
data.writeInt(1);
profilerInfo.writeToParcel(data, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
} else {
data.writeInt(0);
}
if (options != null) {
data.writeInt(1);
options.writeToParcel(data, 0);
} else {
data.writeInt(0);
}
mRemote.transact(START_ACTIVITY_TRANSACTION, data, reply, 0);
reply.readException();
int result = reply.readInt();
reply.recycle();
data.recycle();
return result;
}
...
}
###转到ActivityManagerService-startActivity