您的位置:首页 > 娱乐 > 八卦 > Activity启动相关app的跨进程调用方式

Activity启动相关app的跨进程调用方式

2024/10/7 6:32:02 来源:https://blog.csdn.net/qq_34888036/article/details/141114500  浏览:    关键词:Activity启动相关app的跨进程调用方式

我们知道在启动一个activity时,最终会调用到

 boolean realStartActivityLocked(ActivityRecord r, WindowProcessController proc,boolean andResume, boolean checkConfig) throws RemoteException {
...
// Create activity launch transaction.final ClientTransaction clientTransaction = ClientTransaction.obtain(proc.getThread(), r.token);final boolean isTransitionForward = r.isTransitionForward();final IBinder fragmentToken = r.getTaskFragment().getFragmentToken();clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),System.identityHashCode(r), r.info,// TODO: Have this take the merged configuration instead of separate global// and override configs.mergedConfiguration.getGlobalConfiguration(),mergedConfiguration.getOverrideConfiguration(), r.compat,r.getFilteredReferrer(r.launchedFromPackage), task.voiceInteractor,proc.getReportedProcState(), r.getSavedState(), r.getPersistentSavedState(),results, newIntents, r.takeOptions(), isTransitionForward,proc.createProfilerInfoIfNeeded(), r.assistToken, activityClientController,r.shareableActivityToken, r.getLaunchedFromBubble(), fragmentToken));// Set desired final state.final ActivityLifecycleItem lifecycleItem;if (andResume) {lifecycleItem = ResumeActivityItem.obtain(isTransitionForward);} else {lifecycleItem = PauseActivityItem.obtain();}clientTransaction.setLifecycleStateRequest(lifecycleItem);mService.getLifecycleManager().scheduleTransaction(clientTransaction);
...
}
class ClientLifecycleManager {...void scheduleTransaction(ClientTransaction transaction) throws RemoteException {final IApplicationThread client = transaction.getClient();transaction.schedule();if (!(client instanceof Binder)) {// If client is not an instance of Binder - it's a remote call and at this point it is// safe to recycle the object. All objects used for local calls will be recycled after// the transaction is executed on client in ActivityThread.transaction.recycle();}}...
}

这里的ClientTransaction即

public class ClientTransaction implements Parcelable, ObjectPoolItem {/** A list of individual callbacks to a client. */@UnsupportedAppUsageprivate List<ClientTransactionItem> mActivityCallbacks;/*** Final lifecycle state in which the client activity should be after the transaction is* executed.*/private ActivityLifecycleItem mLifecycleStateRequest;/** Target client. */private IApplicationThread mClient;...public void schedule() throws RemoteException {mClient.scheduleTransaction(this);}}

ClientTransaction 中有两个主要的成员变量mActivityCallbacks和mLifecycleStateRequest。

其中ClientTransactionItem有PauseActivityItem、DestroyActivityItem、ResumeActivityItem、StartActivityItem、StopActivityItem等众多子类,每个子类重写excute方法。这样利于后续拓展,不需要新加aidl接口,只需要拓展类即可。如下:

public class StopActivityItem extends ActivityLifecycleItem {private static final String TAG = "StopActivityItem";private int mConfigChanges;@Overridepublic void execute(ClientTransactionHandler client, ActivityClientRecord r,PendingTransactionActions pendingActions) {Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityStop");client.handleStopActivity(r, mConfigChanges, pendingActions,true /* finalStateRequest */, "STOP_ACTIVITY_ITEM");Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);}
...
}

mclient即applicationThread,mClient.scheduleTransaction(this);会跨进程调用到

//位于ActivityThread.java中
public void scheduleTransaction(ClientTransaction transaction) throws RemoteException {ActivityThread.this.scheduleTransaction(transaction);}

接着调用到activityThread的父类ClientTransactionHandler 的scheduleTransaction中

public abstract class ClientTransactionHandler {// Schedule phase related logic and handlers./** Prepare and schedule transaction for execution. */void scheduleTransaction(ClientTransaction transaction) {transaction.preExecute(this);sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction);}...

然后调用activityThread类的sendMessage

//位于ActivityThread.java中
private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {if (DEBUG_MESSAGES) {Slog.v(TAG,"SCHEDULE " + what + " " + mH.codeToString(what) + ": " + arg1 + " / " + obj);}Message msg = Message.obtain();msg.what = what;msg.obj = obj;msg.arg1 = arg1;msg.arg2 = arg2;if (async) {msg.setAsynchronous(true);}mH.sendMessage(msg);}public void handleMessage(Message msg) {case EXECUTE_TRANSACTION:final ClientTransaction transaction = (ClientTransaction) msg.obj;mTransactionExecutor.execute(transaction);if (isSystem()) {// Client transactions inside system process are recycled on the client side// instead of ClientLifecycleManager to avoid being cleared before this// message is handled.transaction.recycle();}// TODO(lifecycler): Recycle locally scheduled transactions.break;
}
//位于TransactionExecutor.java中
public void execute(ClientTransaction transaction) {if (DEBUG_RESOLVER) Slog.d(TAG, tId(transaction) + "Start resolving transaction");final IBinder token = transaction.getActivityToken();....executeCallbacks(transaction);//执行mActivityCallbacks事件executeLifecycleState(transaction);//执行LifecycleState事件mPendingActions.clear();if (DEBUG_RESOLVER) Slog.d(TAG, tId(transaction) + "End resolving transaction");}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com