博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android源码分析(十三)ActivityManagerService服务分析
阅读量:6002 次
发布时间:2019-06-20

本文共 5048 字,大约阅读时间需要 16 分钟。

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

在SystemServer启动ActivityManagerService

如果想了解SystemServer启动过程可以看这篇文章:

frameworks\base\services\java\com\android\server\SystemServer.java        // Activity manager runs the show.        traceBeginAndSlog("StartActivityManager");        mActivityManagerService = mSystemServiceManager.startService(                ActivityManagerService.Lifecycle.class).getService();        mActivityManagerService.setSystemServiceManager(mSystemServiceManager);        mActivityManagerService.setInstaller(installer);

frameworks\base\services\core\java\com\android\server\am\ActivityManagerService.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
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的过程可以看下这篇文章.

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

转载于:https://www.cnblogs.com/bugzone/p/ActivityManagerService.html

你可能感兴趣的文章
openstack newton 负载均衡Lbaas配置
查看>>
2016年4月7日
查看>>
SpringData+Redis存储
查看>>
C#中用SharpZipLib生成gzip/解压文件
查看>>
JSON Tips
查看>>
Visual Studio 2013 配置OpenGL环境变量
查看>>
iOS开发网络篇之文件下载、大文件下载、断点下载
查看>>
我的友情链接
查看>>
shell:case
查看>>
十三,Eclipse的使用
查看>>
C语言动态分配二维数组
查看>>
Zeppelin集成LDAP权限验证的配置
查看>>
Windows平台上实现P2P服务(四)
查看>>
windows 查看当前是哪些用户在使用共享文件
查看>>
JDK内置工具使用
查看>>
如果你是12306网站架构师,你会如何设计网站的软件架构和硬件系统架构?
查看>>
我的友情链接
查看>>
JSP/Servlet及相关技术详解(一)
查看>>
广播 BroadCastReceiver
查看>>
我的友情链接
查看>>