Android 组件通信中有哪些不为人知的细节?,kotlin编程通俗演义pdf

Activity.startActivity–>Activity.startActivityForResult

–>FragmentActivity.startActivityForResult

–>Instrumentation.execStartActivity

–>ActivityTaskManager.getService().startActivity(xx)

而ActivityTaskManager.getService() 前面已经分析过了,就是获取了AMS对外的接口: IActivityManager,拿到了引用后调用AMS startActivity(xx)方法:

#ActivityManagerService.java

public int startActivity(IApplicationThread caller, String callingPackage,

Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,

int startFlags, ProfilerInfo profilerInfo, Bundle bOptions) {

return mActivityTaskManager.startActivity(caller, callingPackage, intent, resolvedType,

resultTo, resultWho, requestCode, startFlags, profilerInfo, bOptions);

}

重点关注两个参数:IApplicationThread caller和Intent intent。

Intent 我们很熟悉了,指明了要启动哪个Activity。

IApplicationThread 是个接口,里面定义了很多方法,列举部分如:

#IApplicationThread.java

{

//回调静态广播

void scheduleReceiver(in Intent intent, in ActivityInfo info,

in CompatibilityInfo compatInfo,

int resultCode, in String data, in Bundle extras, boolean sync,

int sendingUser, int processState);

@UnsupportedAppUsage

//回调创建服务

void scheduleCreateService(IBinder token, in ServiceInfo info,

in CompatibilityInfo compatInfo, int processState);

@UnsupportedAppUsage

//回调停止服务

void scheduleStopService(IBinder token);

//回调绑定Application

void bindApplication(in String packageName, in ApplicationInfo info,

in ProviderInfoList providerList, in ComponentName testName,

in ProfilerInfo profilerInfo, in Bundle testArguments,

IInstrumentationWatcher testWatcher, IUiAutomationConnection uiAutomationConnection,

int debugMode, boolean enableBinderTracking, boolean trackAllocation,

boolean restrictedBackupMode, boolean persistent, in Configuration config,

in CompatibilityInfo compatInfo, in Map services,

in Bundle coreSettings, in String buildSerial, in AutofillOptions autofillOptions,

in ContentCaptureOptions contentCaptureOptions, in long[] disabledCompatChanges);

//回调Service onStartCommand

void scheduleServiceArgs(IBinder token, in ParceledListSlice args);

//回调绑定服务

void scheduleBindService(IBinder token,

in Intent intent, boolean rebind, int processState);

@UnsupportedAppUsage

//回调解绑服务

void scheduleUnbindService(IBinder token,

in Intent intent);

//回调Activity 生命周期相关

void scheduleTransaction(in ClientTransaction transaction);

}

而ActivityThread.java 里实现了该接口(Android 8.0之后使用了AIDL 定义,此处以此为例分析)。

#ActivityThread.java

private class ApplicationThread extends IApplicationThread.Stub {

//实现了IApplicationThread 接口里定义的方法

}

IApplicationThread 有啥用呢?我们调用AMS方法后,有些方法并没有返回值或者仅仅只返回int,比如startActivity(xx),那么我们的进程如何接收Activity的生命周期的回调呢,不仅Activity的生命周期回调,还有Service等的回调。这个时候就得依靠IApplicationThread 接口回调了。

因此,当调用AMS方法的时候,传入IApplicationThread实例,当AMS完成某个动作后通过IApplicationThread 回调给应用进程。

实际上,AMS里针对每个应用进程只保存了一个IApplicationThread实例,而第一次传递给AMS是在进程启动的时候:

#ActivityThread.java

private void attach(boolean system, long startSeq) {

if (!system) {

//获取AMS引用

final IActivityManager mgr = ActivityManager.getService();

try {

//传入IApplicationThread 实例给AMS

mgr.attachApplication(mAppThread, startSeq);

} catch (RemoteEx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值