Android 内核解剖 - AMS(Activity Manager Service)

news/2024/7/5 18:23:32
zz:http://www.myexception.cn/android/1792465.html
android内核剖析学习笔记:AMS(ActivityManagerService)内部原理和工作机制
      一、ActivityManagerService提供的主要功能:
       (1)统一调度各应用程序的Activity
       (2)内存管理

       (3)进程管理

      二、启动一个Activity的方式有以下几种:
   (1)在应用程序中调用startActivity启动指定的Activity
   (2)在Home程序中单击一个应用图标,启动新的Activity
   (3)按“Back”键,结束当前Activity,返回到上一个Activity
   (4)长按“Home”键,显示出当前正在运行的程序列表,从中选择一个启动
     这四种启动方式的主体处理流程都会按照第一种启动方式运行,后面三种方式只是在前端消息处理上各有不同。

      三、进程数据类ProcessRecord

      该类的源代码在~\frameworks\base\services\java\com\android\server\am路径下。

      一般情况下,一个APK文件运行时会对应一个进程,ProcessRecord用来记录一个进程中的相关信息,主要包含的变量有:
      (1)进程文件信息:与该进程对应的APK文件的内部信息,如

        final ApplicationInfo info; // all about the first app in the process
        final String processName;   // name of the process

        final ArrayMap<String, ProcessStats.ProcessState> pkgList 
            = new ArrayMap<String, ProcessStats.ProcessState>();   //保存进程中所有APK文件包名

      (2)进程的内存状态信息:用于Linux系统的out of memory(OOM)情况的处理,当发生内存紧张时,Linux系统会根据进程的内存状态信息杀掉低优先级的进程,包括的变量有

        int maxAdj;                 // Maximum OOM adjustment for this process
        int curRawAdj;              // Current OOM unlimited adjustment for this process
        int setRawAdj;              // Last set OOM unlimited adjustment for this process
        int curAdj;                 // Current OOM adjustment for this process
        int setAdj;                 // Last set OOM adjustment for this process
       变量中Adj的含义是调整值(adjustment)

     (3)进程中包含的Activity、Provider、Service等,如下

       final ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
       final ArraySet<ServiceRecord> services = new ArraySet<ServiceRecord>();
       final ArraySet<ServiceRecord> executingServices = new ArraySet<ServiceRecord>();
       final ArraySet<ConnectionRecord> connections = new ArraySet<ConnectionRecord>();
       final ArraySet<ReceiverList> receivers = new ArraySet<ReceiverList>();
       final ArrayMap<String, ContentProviderRecord> pubProviders = new ArrayMap<String,             ContentProviderRecord>();
    final ArrayList<ContentProviderConnection> conProviders = new ArrayList<ContentProviderConnection>();

     四、ActivityRecord数据类(Android 2.3以前版本叫HistoryRecord类)

     ActivityManagerService使用ActivityRecord数据类来保存每个Activity的信息,ActivityRecord类基于IApplicationToken.Stub类,也是一个Binder,所以可以被IPC调用。

     主要包含的变量有:

   (1)环境信息:Activity的工作环境,比如进程名称、文件路径、数据路径、图标、主题等,这些信息一般是固定的,比如以下变量

    final String packageName; // the package implementing intent's component
    final String processName; // process where this component wants to run
    final String baseDir;   // where activity source (resources etc) located
    final String resDir;   // where public activity source (public resources etc) located
    final String dataDir;   // where activity data should go
    int theme;              // resource identifier of activity's theme.
    int realTheme;          // actual theme resource we will use, never 0.

   (2)运行状态数据信息:如idle、stop、finishing等,一般为boolean类型,如下

    boolean haveState;      // have we gotten the last activity state?
    boolean stopped;        // is activity pause finished?
    boolean delayedResume;  // not yet resumed because of stopped app switches?
    boolean finishing;      // activity in pending finish list?
    boolean configDestroy;  // need to destroy due to config change?

   五、TaskRecord类

    ActivityManagerService中使用任务的概念来确保Activity启动和退出的顺序。
    TaskRecord中的几个重要变量如下:
    final int taskId;       // 每个任务的标识.
    Intent intent;          // 创建该任务时对应的intent
    int numActivities;   //该任务中的Activity数目
    final ArrayList<ActivityRecord> mActivities = new ArrayList<ActivityRecord>();  //按照出现的先后顺序列出该任务中的所有Activity

     六、ActivityManagerService中一些重要的与调度相关的变量

    (1)记录最近启动的Activity,如果RAM容量较小,则记录的最大值为10个,否则为20个,超过该值后,Ams会舍弃最早记录的Activity
      static final int MAX_RECENT_TASKS = ActivityManager.isLowRamDeviceStatic() ? 10 : 20;
    (2)当Ams通知应用程序启动(Launch)某个Activity时,如果超过10s,Ams就会放弃
    static final int PROC_START_TIMEOUT = 10*1000;
    (3)当Ams启动某个客户进程后,客户进程必须在10s之内报告Ams自己已经启动,否则Ams会认为指定的客户进程不存在
    static final int PROC_START_TIMEOUT = 10*1000;
    (4)等待序列:
    当Ams内部还没有准备好时,如果客户进程请求启动某个Activity,那么会被暂时保存到该变量中,
      final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
            = new ArrayList<PendingActivityLaunch>();
    (5)优先启动,其次再停止。进程A1包含两个Activity,启动顺序为A1->A2,当用户请求启动A2时,如果A1正在运行,Ams会先暂停A1,然后启动A2,当A2启动后再停止A1。
       private final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>();

     七、startActivity()的流程
       当用户单击某个应用图标后,执行程序会在该图标的onClick()事件中调用startActivity()方法,该方法会调用startActivityForResult(),在这个方法内部会调用Instrumentation对象的executeStartActivity()方法,每个Activity内部都有一个Instrumentation对象的引用,它就是一个管家,ActivityThread要创建或者暂停某个Activity都是通过它实现的。

      流程图如下所示:

 流程图如下所示:



   原网址:<a target=_blank href="http://popohope.blog.chinaunix.net/uid-29043620-id-5194763.html">http://popohope.blog.chinaunix.net/uid-29043620-id-5194763.html</a>(此网址附有AMS源码)

                

http://www.niftyadmin.cn/n/3649380.html

相关文章

CentOS 7安装MySQL-5.7数据库

MySQL是一个关系型数据库管理系统&#xff0c;由瑞典MySQL AB 公司开发&#xff0c;属于Oracle旗下产品。MySQL是最流行的关系型数据库管理系统之一&#xff0c;在WEB应用方面&#xff0c;MySQL是最好的RDBMS (Relational Database Management System&#xff0c;关系数据库管理…

bootstrap 小程序_如何将Bootstrap添加到Ruby on Rails应用程序

bootstrap 小程序介绍 (Introduction) If you are developing a Ruby on Rails application, you may be interested in adding styles to your project to facilitate user engagement. One way to do this is by adding Bootstrap, an HTML, CSS, and JavaScript framework d…

server——短信提醒

一、首先需要开启服务 startService(new Intent(MainActivity.this,MyService.class)); 开启肯定也需要关闭服务 stopService(new Intent(MainActivity.this,MyService.class)); 不要忘了在manifest中注册: <service android:name"com.dongge.service.MyService&…

[C#]I/O完成端口的类定义和测试实例

从William Kennedy那里整理过来的,不同之处在于他自己定义了一个Overlapped&#xff0c;而我们这里直接使用 System.Threading.NativeOverlapped.附一段我以前的Win32下的IOCP文档&#xff0c;如果您了解IOCP也可以直接跳过看后面的C#测试示范&#xff1a;整理者&#xff1a;郑…

关于“微笑涛声”博客

博客简介“微笑涛声”是一个分享学习心得、记录自己生活点滴、探索互联网技术的博客。随着互联网技术的飞速发展&#xff0c;在互联网上留下属于自己的足迹已不再是难事。能在互联网上拥有一个真正属于自己的空间&#xff0c;是我的梦想&#xff0c;而今天这个梦想在“博客”中…

检测Android应用(APP)的启动与关闭

问题 当开发安卓程序的时候&#xff0c;我们不免需要去检测应用什么时候在前台运行&#xff0c;用户什么时候离开。不幸的是&#xff0c;没有一个简单的方法可以做到这点。当用户第一次启动的时候去检测还是不难&#xff0c;但如果是重新打开或关闭就不简单了。 这篇文章将会展…

vue项目构建和部署_如何使用Vue设置,构建和部署本机应用程序

vue项目构建和部署Vue Native is a Javascript framework that is designed to deliver cross platform mobile native applications. It is inspired by the React Native project. Vue Native是一个Javascript框架&#xff0c;旨在提供跨平台的移动本机应用程序。 它受React …

智能管家(一)

#第一章&#xff1a;项目的准备##一、项目的第一步导入包compile com.android.support:design:24.2.1软件包分类:com.yijia.com.smartbutler.adaptercom.yijia.com.smartbutler.applicationcom.yijia.com.smartbutler.entitycom.yijia.com.smartbutler.fragmentcom.yijia.com.s…