ImageVerifierCode 换一换
格式:DOCX , 页数:46 ,大小:62.32KB ,
资源ID:11389143      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11389143.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Android应用程序启动过程源代码分析.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Android应用程序启动过程源代码分析.docx

1、Android应用程序启动过程源代码分析Android应用程序启动过程源代码分析前文简要介绍了Android应用程序的Activity的启动过程。在Android系统中,应用程序是由Activity组成的,因此,应用程序的启动过程实际上就是应用程序中的默认Activity的启动过程,本文将详细分析应用程序框架层的源代码,了解Android应用程序的启动过程。在上一篇文章Android应用程序的Activity启动过程简要介绍和学习计划中,我们举例子说明了启动Android应用程序中的Activity的两种情景,其中,在手机屏幕中点击应用程序图标的情景就会引发Android应用程序中的默认Act

2、ivity的启动,从而把应用程序启动起来。这种启动方式的特点是会启动一个新的进程来加载相应的Activity。这里,我们继续以这个例子为例来说明Android应用程序的启动过程,即MainActivity的启动过程。MainActivity的启动过程如下图所示:下面详细分析每一步是如何实现的。Step 1. Launcher.startActivitySafely在Android系统中,应用程序是由Launcher启动起来的,其实,Launcher本身也是一个应用程序,其它的应用程序安装后,就会Launcher的界面上出现一个相应的图标,点击这个图标时,Launcher就会对应的应用程序启动起

3、来。Launcher的源代码工程在packages/apps/Launcher2目录下,负责启动其它应用程序的源代码实现在src/com/android/launcher2/Launcher.java文件中:/* Default launcher application.*/public final class Launcher extends Activityimplements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher ./* Launches the in

4、tent referred by the clicked shortcut.* param v The view representing the clicked shortcut.*/public void onClick(View v) Object tag = v.getTag();if (tag instanceof ShortcutInfo) / Open shortcutfinal Intent intent = (ShortcutInfo) tag).intent;int pos = new int2;v.getLocationOnScreen(pos);intent.setSo

5、urceBounds(new Rect(pos0, pos1,pos0 + v.getWidth(), pos1 + v.getHeight();startActivitySafely(intent, tag); else if (tag instanceof FolderInfo) . else if (v = mHandleView) .void startActivitySafely(Intent intent, Object tag) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);try startActivity(intent); ca

6、tch (ActivityNotFoundException e) . catch (SecurityException e) . 回忆一下前面一篇文章Android应用程序的Activity启动过程简要介绍和学习计划说到的应用程序Activity,它的默认Activity是MainActivity,这里是AndroidManifest.xml文件中配置的: 因此,这里的intent包含的信息为:action = android.intent.action.Main,category=android.intent.category.LAUNCHER, cmp=shy.luo.activity/

7、.MainActivity,表示它要启动的Activity为shy.luo.activity.MainActivity。Intent.FLAG_ACTIVITY_NEW_TASK表示要在一个新的Task中启动这个Activity,注意,Task是Android系统中的概念,它不同于进程Process的概念。简单地说,一个Task是一系列Activity的集合,这个集合是以堆栈的形式来组织的,遵循后进先出的原则。事实上,Task是一个非常复杂的概念,有兴趣的读者可以到官网Step 2. Activity.startActivity在Step 1中,我们看到,Launcher继承于Activity

8、类,而Activity类实现了startActivity函数,因此,这里就调用了Activity.startActivity函数,它实现在frameworks/base/core/java/android/app/Activity.java文件中:view plainpublic class Activity extends ContextThemeWrapper implements LayoutInflater.Factory, Window.Callback, KeyEvent.Callback, OnCreateContextMenuListener, ComponentCallbac

9、ks . Override public void startActivity(Intent intent) startActivityForResult(intent, -1); . 这个函数实现很简单,它调用startActivityForResult来进一步处理,第二个参数传入-1表示不需要这个Actvity结束后的返回结果。Step 3. Activity.startActivityForResult这个函数也是实现在frameworks/base/core/java/android/app/Activity.java文件中:view plainpublic class Activit

10、y extends ContextThemeWrapper implements LayoutInflater.Factory, Window.Callback, KeyEvent.Callback, OnCreateContextMenuListener, ComponentCallbacks . public void startActivityForResult(Intent intent, int requestCode) if (mParent = null) Instrumentation.ActivityResult ar = mInstrumentation.execStart

11、Activity( this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode); . else . . 这里的mInstrumentation是Activity类的成员变量,它的类型是Intrumentation,定义在frameworks/base/core/java/android/app/Instrumentation.java文件中,它用来监控应用程序和系统的交互。这里的mMainThread也是Activity类的成员变量,它的类型是ActivityThread,它代表的是应用程序的主线程,

12、我们在Android系统在新进程中启动自定义服务过程(startService)的原理分析一文中已经介绍过了。这里通过mMainThread.getApplicationThread获得它里面的ApplicationThread成员变量,它是一个Binder对象,后面我们会看到,ActivityManagerService会使用它来和ActivityThread来进行进程间通信。这里我们需注意的是,这里的mMainThread代表的是Launcher应用程序运行的进程。这里的mToken也是Activity类的成员变量,它是一个Binder对象的远程接口。Step 4. Instrumenta

13、tion.execStartActivity这个函数定义在frameworks/base/core/java/android/app/Instrumentation.java文件中:view plainpublic class Instrumentation . public ActivityResult execStartActivity( Context who, IBinder contextThread, IBinder token, Activity target, Intent intent, int requestCode) IApplicationThread whoThrea

14、d = (IApplicationThread) contextThread; if (mActivityMonitors != null) . try int result = ActivityManagerNative.getDefault() .startActivity(whoThread, intent, intent.resolveTypeIfNeeded(who.getContentResolver(), null, 0, token, target != null ? target.mEmbeddedID : null, requestCode, false, false);

15、. catch (RemoteException e) return null; . 这里的ActivityManagerNative.getDefault返回ActivityManagerService的远程接口,即ActivityManagerProxy接口,具体可以参考Android系统在新进程中启动自定义服务过程(startService)的原理分析一文。这里的intent.resolveTypeIfNeeded返回这个intent的MIME类型,在这个例子中,没有AndroidManifest.xml设置MainActivity的MIME类型,因此,这里返回null。这里的targe

16、t不为null,但是target.mEmbddedID为null,我们不用关注。Step 5. ActivityManagerProxy.startActivity这个函数定义在frameworks/base/core/java/android/app/ActivityManagerNative.java文件中:view plainclass ActivityManagerProxy implements IActivityManager . public int startActivity(IApplicationThread caller, Intent intent, String re

17、solvedType, Uri grantedUriPermissions, int grantedMode, IBinder resultTo, String resultWho, int requestCode, boolean onlyIfNeeded, boolean debug) throws RemoteException Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); data.writeInterfaceToken(IActivityManager.descriptor); data.writeStr

18、ongBinder(caller != null ? caller.asBinder() : null); intent.writeToParcel(data, 0); data.writeString(resolvedType); data.writeTypedArray(grantedUriPermissions, 0); data.writeInt(grantedMode); data.writeStrongBinder(resultTo); data.writeString(resultWho); data.writeInt(requestCode); data.writeInt(on

19、lyIfNeeded ? 1 : 0); data.writeInt(debug ? 1 : 0); mRemote.transact(START_ACTIVITY_TRANSACTION, data, reply, 0); reply.readException(); int result = reply.readInt(); reply.recycle(); data.recycle(); return result; . 这里的参数比较多,我们先整理一下。从上面的调用可以知道,这里的参数resolvedType、grantedUriPermissions和resultWho均为null;

20、参数caller为ApplicationThread类型的Binder实体;参数resultTo为一个Binder实体的远程接口,我们先不关注它;参数grantedMode为0,我们也先不关注它;参数requestCode为-1;参数onlyIfNeeded和debug均空false。Step 6. ActivityManagerService.startActivity上一步Step 5通过Binder驱动程序就进入到ActivityManagerService的startActivity函数来了,它定义在frameworks/base/services/java/com/android/s

21、erver/am/ActivityManagerService.java文件中:view plainpublic final class ActivityManagerService extends ActivityManagerNative implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback . public final int startActivity(IApplicationThread caller, Intent intent, String resolvedType, Uri grantedUriPermis

22、sions, int grantedMode, IBinder resultTo, String resultWho, int requestCode, boolean onlyIfNeeded, boolean debug) return mMainStack.startActivityMayWait(caller, intent, resolvedType, grantedUriPermissions, grantedMode, resultTo, resultWho, requestCode, onlyIfNeeded, debug, null, null); . 这里只是简单地将操作转

23、发给成员变量mMainStack的startActivityMayWait函数,这里的mMainStack的类型为ActivityStack。Step 7. ActivityStack.startActivityMayWait这个函数定义在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:view plainpublic class ActivityStack . final int startActivityMayWait(IApplicationThread caller, Intent in

24、tent, String resolvedType, Uri grantedUriPermissions, int grantedMode, IBinder resultTo, String resultWho, int requestCode, boolean onlyIfNeeded, boolean debug, WaitResult outResult, Configuration config) . boolean componentSpecified = intent.getComponent() != null; / Dont modify the clients object!

25、 intent = new Intent(intent); / Collect information about the target of the Intent. ActivityInfo aInfo; try ResolveInfo rInfo = AppGlobals.getPackageManager().resolveIntent( intent, resolvedType, PackageManager.MATCH_DEFAULT_ONLY | ActivityManagerService.STOCK_PM_FLAGS); aInfo = rInfo != null ? rInf

26、o.activityInfo : null; catch (RemoteException e) . if (aInfo != null) / Store the found target back into the intent, because now that / we have it we never want to do this again. For example, if the / user navigates back to this point in the history, we should / always restart the exact same activit

27、y. intent.setComponent(new ComponentName( aInfo.applicationInfo.packageName, aInfo.name); . synchronized (mService) int callingPid; int callingUid; if (caller = null) . else callingPid = callingUid = -1; mConfigWillChange = config != null & mService.mConfiguration.diff(config) != 0; . if (mMainStack

28、 & aInfo != null & (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) . int res = startActivityLocked(caller, intent, resolvedType, grantedUriPermissions, grantedMode, aInfo, resultTo, resultWho, requestCode, callingPid, callingUid, onlyIfNeeded, componentSpecified); if (mConfi

29、gWillChange & mMainStack) . . if (outResult != null) . return res; . 注意,从Step 6传下来的参数outResult和config均为null,此外,表达式(aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0为false,因此,这里忽略了无关代码。下面语句对参数intent的内容进行解析,得到MainActivity的相关信息,保存在aInfo变量中:view plainActivityInfo aInfo; try ResolveInfo rInfo = AppGlobals.getPackageManager().resolveIntent( intent, resolvedType, PackageManager.MATCH_DEFAULT_O

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1