startActivity涉及到的接口和类以及最终实现的源码

startActivity涉及到的接口和类以及最终实现的源码_第1张图片
startActivitySource.jpg

最终的实现是在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);
         }
         mRemote.transact(START_ACTIVITY_TRANSACTION, data, reply, 0);
         reply.readException();
         int result = reply.readInt();
         reply.recycle();
         data.recycle();
         return result;
     }

通过上面的源码可以看到startActivity是需要用到Binder的,而android的进程间通讯用的就是Binder,由此可见startActivity是通过进程间通讯机制实现的。

其中的mRemote的来源参考以下源码

class  ActivityManagerProxy implements IActivityManager
 {
     public More ...ActivityManagerProxy(IBinder remote)
     {
         mRemote = remote;
     }
 
     public IBinder asBinder()
     {
         return mRemote;
     }

你可能感兴趣的:(startActivity涉及到的接口和类以及最终实现的源码)