zoukankan      html  css  js  c++  java
  • Android源码分析(十三)ActivityManagerService服务分析

    一.ActivityManagerService(AMS) 启动过程分析

    在SystemServer启动ActivityManagerService

    如果想了解SystemServer启动过程可以看这篇文章:Android 源码分析(六) SystemServer 进程

    frameworksbaseservicesjavacomandroidserverSystemServer.java
            // Activity manager runs the show.
            traceBeginAndSlog("StartActivityManager");
            mActivityManagerService = mSystemServiceManager.startService(
                    ActivityManagerService.Lifecycle.class).getService();
            mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
            mActivityManagerService.setInstaller(installer);
            

    frameworksaseservicescorejavacomandroidserveramActivityManagerService.java

    public class ActivityManagerService extends IActivityManager.Stub
            implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
    
        /** All system services */
        SystemServiceManager mSystemServiceManager;
        
        /** Run all ActivityStacks through this */
        //管理Activity
        final ActivityStackSupervisor mStackSupervisor;
        
        final ActivityStarter mActivityStarter;
        
        final TaskChangeNotificationController mTaskChangeNotificationController;
    
        final InstrumentationReporter mInstrumentationReporter = new InstrumentationReporter();
    
        final ArrayList<ActiveInstrumentation> mActiveInstrumentation = new ArrayList<>();
        
        // Whether we should use SCHED_FIFO for UI and RenderThreads.
        private boolean mUseFifoUiScheduling = false;
     
        //Broadcast 广播 ,前台广播队列和后台广播队列
        BroadcastQueue mFgBroadcastQueue;
        BroadcastQueue mBgBroadcastQueue;
        
        // Convenient for easy iteration over the queues. Foreground is first
        // so that dispatch of foreground broadcasts gets precedence.
        final BroadcastQueue[] mBroadcastQueues = new BroadcastQueue[2];
    
        BroadcastStats mLastBroadcastStats;
        BroadcastStats mCurBroadcastStats;
    
        BroadcastQueue broadcastQueueForIntent(Intent intent) {
            final boolean isFg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0;
            if (DEBUG_BROADCAST_BACKGROUND) Slog.i(TAG_BROADCAST,
                    "Broadcast intent " + intent + " on "
                    + (isFg ? "foreground" : "background") + " queue");
            return (isFg) ? mFgBroadcastQueue : mBgBroadcastQueue;
        }
    
        //Activity 堆栈
        /**
         * The last resumed activity. This is identical to the current resumed activity most
         * of the time but could be different when we're pausing one activity before we resume
         * another activity.
         */
        private ActivityRecord mLastResumedActivity;
    
        /**
         * If non-null, we are tracking the time the user spends in the currently focused app.
         */
        private AppTimeTracker mCurAppTimeTracker;
        
        //ANR ? 最后个ANR状态,难道可以记录app发生ANR的?
        /**
         * Dump of the activity state at the time of the last ANR. Cleared after
         * {@link WindowManagerService#LAST_ANR_LIFETIME_DURATION_MSECS}
         */
        String mLastANRState;
        
        //Service 和 Provider 管理
        final ActiveServices mServices;
        final ProviderMap mProviderMap;
        
        //存放系统数据目录
        // TODO: Move creation of battery stats service outside of activity manager service.
        File dataDir = Environment.getDataDirectory();
        File systemDir = new File(dataDir, "system");
        systemDir.mkdirs();
        mBatteryStatsService = new BatteryStatsService(systemDir, mHandler);
    
        //应用权限管理
        mAppOpsService = mInjector.getAppOpsService(new File(systemDir, "appops.xml"), mHandler);
        
            //AcitivityManager 添加进来
            ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);
                    ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
                    ServiceManager.addService("meminfo", new MemBinder(this));
                    ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
                    ServiceManager.addService("dbinfo", new DbBinder(this));
                    if (MONITOR_CPU_USAGE) {
                        ServiceManager.addService("cpuinfo", new CpuBinder(this));
                    }
                    ServiceManager.addService("permission", new PermissionController(this));
                    ServiceManager.addService("processinfo", new ProcessInfoService(this));
        
        //最后 使用Watchdog监控
        Watchdog.getInstance().addMonitor(this);
        Watchdog.getInstance().addThread(mHandler);
        
    }

       从上面截取的一些代码片段,我们能了解到, AMS创建过程 涉及到Android 四大组件管理的初始化工作。并且ActivityManagerService extends IActivityManager.Stub,所以可知AcitivityManagerService与AcitivityManager之间通信也是使用binder机制。

        进ActivityManager 里面看看

    //与ActivityManagerService里的ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);对应。
    @SystemService(Context.ACTIVITY_SERVICE)
    public class ActivityManager {
        ...
    }

    二.ActivityManager和ActivityManagerService关系

    如果想了解Activity是如果通过ActivityManager调用ActivityManagerService的过程可以看下这篇文章.

    Android 源码分析(二) Activity 启动分析

    ActivityManager(frameworks/base/core/java/android/app/ActivityManager.java)

    ActivityManager 是客户端用来管理系统中正在运行的所有Activity.

    上层APP通过ActivityManager使用binder机制传递信息给AMS,由AMS去完成交互和调度工作后通过binder机制返回给ActivityManager,把结果在返回给上层app。

    一张图了解ActivityManager和ActivityManagerService在整个Android系统通信过程中位置。

    参考:
    [1]https://www.cnblogs.com/bastard/p/5770573.html

  • 相关阅读:
    bzoj 1761: [Baltic2009]beetle 区间dp
    NOI冲刺计划
    bzoj 2107: Spoj2832 Find The Determinant III 辗转相除法
    bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树
    bzoj 1209: [HNOI2004]最佳包裹 三维凸包
    SCOI2015题解 && 考试小结
    bzoj 2806: [Ctsc2012]Cheat 后缀自动机DP
    考场上应该想到。。。。
    spoj LCS 后缀自动机
    BZOJ 1639: [Usaco2007 Mar]Monthly Expense 月度开支
  • 原文地址:https://www.cnblogs.com/bugzone/p/ActivityManagerService.html
Copyright © 2011-2022 走看看