Android5.0 Framework - Activity启动过程(二)
标签(空格分隔): Android Framework
1. 前言
在Android Framework - Activity启动过程(一)中追踪源码到Instrumentation类中,那么Instrumentation到底如何启动Activity?
2. Instrumentation类
public ActivityResult execStartActivity( Context who, IBinder contextThread, IBinder token, Activity target, Intent intent, int requestCode, Bundle options) { IApplicationThread whoThread = (IApplicationThread) contextThread; if (mActivityMonitors != null) { synchronized (mSync) { //遍历查询,是否存在这个Activity final int N = mActivityMonitors.size(); for (int i=0; i<N; i++) { final ActivityMonitor am = mActivityMonitors.get(i); if (am.match(who, null, intent)) { am.mHits++; //Activity如果打不开,这直接return if (am.isBlocking()) { return requestCode >= 0 ? am.getResult() : null; } break; } } } } try { intent.migrateExtraStreamToClipData(); intent.prepareToLeaveProcess(); //打开activity //ActivityManagerNative.getDefault()就是ActivityManagerProxy对象 //ActivityManagerProxy就是进程通信代理对象 //ActivityManagerProxy对象再通过IBinder关联到ActivityManagerService int result = ActivityManagerNative.getDefault() .startActivity(whoThread, who.getBasePackageName(), intent, intent.resolveTypeIfNeeded(who.getContentResolver()), token, target != null ? target.mEmbeddedID : null, requestCode, 0, null, options); //检测打开的activity,会抛各种异常 checkStartActivityResult(result, intent); } catch (RemoteException e) { } return null; }
- ActivityManagerNative.getDefault()返回的是ActivityManagerProxy对象
- 重点在于ActivityManagerNative.getDefault()
.startActivity(…)方法- 顺便看一下 checkStartActivityResult(result, intent);是干什么的?
那么ActivityManagerNative.getDefault()为什么返回的是ActivityManagerProxy对象呢?那么打开ActivityManagerNative源码来看一下
/** * Retrieve the system's default/global activity manager. */ static public IActivityManager getDefault() { return gDefault.get(); }
那么gDefault又是什么呢?返回的是IActivityManager
private static final Singleton<IActivityManager> gDefault = new Singleton<IActivityManager>() { protected IActivityManager create() { //代理类 IBinder b = ServiceManager.getService("activity"); if (false) { Log.v("ActivityManager", "default service binder = " + b); } IActivityManager am = asInterface(b); if (false) { Log.v("ActivityManager", "default service = " + am); } return am; } };}
那么IActivityManager又是什么呢?
static public IActivityManager asInterface(IBinder obj) { if (obj == null) { return null; } IActivityManager in = (IActivityManager)obj.queryLocalInterface(descriptor); if (in != null) { return in; } return new ActivityManagerProxy(obj); }
从源码中 return new ActivityManagerProxy(obj);可见返回的就是 ActivityManagerProxy对象;
2.checkStartActivityResult(result, intent)
public static void checkStartActivityResult(int res, Object intent) { if (res >= ActivityManager.START_SUCCESS) { return; } switch (res) { case ActivityManager.START_INTENT_NOT_RESOLVED: case ActivityManager.START_CLASS_NOT_FOUND: if (intent instanceof Intent && ((Intent)intent).getComponent() != null) //在xml中没有注册activity throw new ActivityNotFoundException( "Unable to find explicit activity class " + ((Intent)intent).getComponent().toShortString() + "; have you declared this activity in your AndroidManifest.xml?"); throw new ActivityNotFoundException( "No Activity found to handle " + intent); case ActivityManager.START_PERMISSION_DENIED: throw new SecurityException("Not allowed to start activity " + intent); case ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT: throw new AndroidRuntimeException( "FORWARD_RESULT_FLAG used while also requesting a result"); case ActivityManager.START_NOT_ACTIVITY: throw new IllegalArgumentException( "PendingIntent is not an activity"); case ActivityManager.START_NOT_VOICE_COMPATIBLE: throw new SecurityException( "Starting under voice control not allowed for: " + intent); default: throw new AndroidRuntimeException("Unknown error code " + res + " when starting " + intent); } }
- 仔细一看,原打开activity的时候,这个方法检测异常的
- 其中常见的 Unable to find e···have you declared this activity in your AndroidManifest.xml?就是在xm中没有找到打开的Activity,需要在AndroidManifestxml中注册
3.ActivityManagerProxy类,说在前面
1. 打开ActivityManagerProxy类,发现是ActivityManagerNative类的内部类;
2. ActivityManagerProxy和ActivityManagerNative都实现IActivityManager这个接口
3. ActivityManagerNative是抽象类,ActivityManagerService继承ActivityManagerNative
3.ActivityManagerProxy类的实现
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); } //通过Binder驱动程序就进入到ActivityManagerService的startActivity函数来 //mRemote是一个BinderProxy对象,transact方法本地化实现 mRemote.transact(START_ACTIVITY_TRANSACTION, data, reply, 0); reply.readException(); int result = reply.readInt(); reply.recycle(); data.recycle(); return result; }
这里并没有处理,只是个代理,通过IBinder,最终将消息传递到ActivityManagerService中,让ActivityManagerService处理打开Activity.
那么ActivityManagerService到底如何启动Activity待续…
版权声明:本文为博主原创文章,未经博主允许不得转载。