77范文网 - 专业文章范例文档资料分享平台

Activtiy完全解析(一、Activity的创建过程)(2)

来源:网络收集 时间:2019-02-16 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

reply.readException(); int result = reply.readInt(); reply.recycle(); data.recycle(); return result; }

上面说到ActivityManagerNative才是真正干活的,他维护了ActivityManagerService的远程代理对象mRemote ,最终会通过mRemote将开启

Activity的消息传送给ActivityManagerService,这样就来到了ActivityManagerService的startActivity方法中:

step4. ActivityManagerService

public final class ActivityManagerService extends ActivityManagerNative implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { ...

@Override

public final int startActivity(IApplicationThread caller, String callingPackage,

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

int startFlags, ProfilerInfo profilerInfo, Bundle options) {

return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo, resultWho, requestCode, startFlags, profilerInfo, options, UserHandle.getCallingUserId()); }

@Override

public final int startActivityAsUser(IApplicationThread caller, String callingPackage,

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

requestCode,

int startFlags, ProfilerInfo profilerInfo, Bundle options, int userId) {

enforceNotIsolatedCaller(\

userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, false, ALLOW_FULL_ONLY, \ //mStackSupervisor的类型是ActivityStackSupervisor

return mStackSupervisor.startActivityMayWait(caller, -1, callingPackage, intent, resolvedType, null, null, resultTo, resultWho, requestCode, startFlags, profilerInfo, null, null, options, userId, null, null);

}

... }

ActivityManagerService中startActivity()直接调用了startActivityAsUser()方法,这个方法中又调用了ActivitySupervisor的

startActivityMayWait()方法:

step5. ActivityStackSupervisor.startActivityMayWait()

final int startActivityMayWait(IApplicationThread caller, int callingUid,

String callingPackage, Intent intent, String resolvedType, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,

IBinder resultTo, String resultWho, int requestCode, int startFlags,

ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,

Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) { ...

// Don't modify the client's object! intent = new Intent(intent);

// 调用resolveActivity()根据意图intent参数,解析目标Activity的一些信息保存到aInfo中,

// 这些信息包括activity的name、applicationInfo、processName、theme、launchMode、permission、flags等等

// 这都是在AndroidManifest.xml中为activity配置的

ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);

...

synchronized (mService) {

//下面省略的代码用于重新组织startActivityLocked()方法需要的参数 ...

//调用startActivityLocked开启目标activity

int res = startActivityLocked(caller, intent, resolvedType, aInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode, callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags, options, componentSpecified, null, container, inTask);

...

if (outResult != null) {

//如果outResult不为null,则设置开启activity的结果 outResult.result = res; ...

return res; } }

根据上面传递的参数和应用信息重新封装一些参数,然后调用startActivityLocked()方法:

step6. ActivityStackSupervisor.startActivityLocked()

final int startActivityLocked(IApplicationThread caller,

Intent intent, String resolvedType, ActivityInfo aInfo,

IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,

IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid, String callingPackage,

int realCallingPid, int realCallingUid, int startFlags, Bundle options,

boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,

TaskRecord inTask) { int err = ActivityManager.START_SUCCESS;

//调用者的进程信息,也就是哪个进程要开启此Activity的 ProcessRecord callerApp = null;

//下面有很多if语句,用于判断一些错误信息,并给err赋值相应的错误码 if (caller != null) {

callerApp = mService.getRecordForAppLocked(caller); if (callerApp != null) {

callingPid = callerApp.pid;

callingUid = callerApp.info.uid; } else {

err = ActivityManager.START_PERMISSION_DENIED; } } ...

if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) { err = ActivityManager.START_INTENT_NOT_RESOLVED; }

if (err == ActivityManager.START_SUCCESS && aInfo == null) { // 未找到需要打开的activity的class文件

err = ActivityManager.START_CLASS_NOT_FOUND; } ...

//上面判断完成之后,接着判断如果err不为START_SUCCESS,则说明开启activity失败,直接返回错误码

if (err != ActivityManager.START_SUCCESS) { if (resultRecord != null) {

resultStack.sendActivityResultLocked(-1,

resultRecord, resultWho, requestCode, Activity.RESULT_CANCELED, null); }

ActivityOptions.abort(options); return err; }

//检查权限,有些activity在清单文件中注册了权限,比如要开启系统相机,就需要注册相机权限,否则此处就会跑出异常

final int startAnyPerm = mService.checkPermission(

START_ANY_ACTIVITY, callingPid, callingUid);

final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,

callingUid, aInfo.applicationInfo.uid, aInfo.exported);

if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {

if (resultRecord != null) {

resultStack.sendActivityResultLocked(-1,

resultRecord, resultWho, requestCode, Activity.RESULT_CANCELED, null); }

String msg;

//权限被拒绝,抛出异常 if (!aInfo.exported) {

msg = \ + \ + \

+ \ } else {

msg = \ + \ + \ + \ }

throw new SecurityException(msg); }

//★★★经过上面的判断后,创建一个新的Activity记录,

// 这个ActivityRecord就是被创建的activity在历史堆栈中的一个条目,表示一个活动 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage, intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho, requestCode, componentSpecified, this, container, options); ...

//继续调用startActivityUncheckedLocked()

err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor, startFlags, true, options, inTask); ...

return err; }

这个方法主要是判断一些错误信息和检查权限,如果没有发现错误(err==START_SUCCESS)就继续开启activity, 否则直接返回错误码。继续

查看startActivityUnChechedLocked()方法:

step7 : ActivityStackSupervisor.startActivityUncheckedLocked()

final int startActivityUncheckedLocked(ActivityRecord r, ActivityRecord sourceRecord, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int

startFlags,

boolean doResume, Bundle options, TaskRecord inTask) { ...

//① 获取并配置activity配置的启动模式 int launchFlags = intent.getFlags();

if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && (launchSingleInstance || launchSingleTask)) { launchFlags &=

~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); } else { ... } ... /*

* 如果调用者不是来自另一个activity(不是在activity中调用startActivity), * 但是给了我们用于放入心activity的一个明确的task,将执行下面代码 *

* 我们往上追溯,发现inTask是step4 中 ActivityManagerService.startActivityAsUser()

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Activtiy完全解析(一、Activity的创建过程)(2)在线全文阅读。

Activtiy完全解析(一、Activity的创建过程)(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/474392.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: