Android4_SRC_分析~ActivityManagerService~图解(一)

OK,从现在开始,我们会一个要点一个要点的把Android目前在开发上比较中要的几个知识点从源码开始详细分析,当然,在这里,我主要的是分析framework层下的代码,今天我给分析是关于ActivityManagerService.java这个类,分析内容主要为其内部原理,由于本类代码比较多,所以我不可能在一个篇幅中全部给细细分析与展示出来,所以我主要分析其内部原理:

好了先看下面:

package com.android.server.am;

   18 

   19 import com.android.internal.R;

   20 import com.android.internal.os.BatteryStatsImpl;

   21 import com.android.internal.os.ProcessStats;

   22 import com.android.server.AttributeCache;

   23 import com.android.server.IntentResolver;

   24 import com.android.server.ProcessMap;

   25 import com.android.server.SystemServer;

   26 import com.android.server.Watchdog;

   27 import com.android.server.am.ActivityStack.ActivityState;

   28 import com.android.server.wm.WindowManagerService;

   30 import dalvik.system.Zygote;

   32 import android.app.Activity;

   33 import android.app.ActivityManager;

   34 import android.app.ActivityManagerNative;

   35 import android.app.ActivityThread;

   36 import android.app.AlertDialog;

   37 import android.app.AppGlobals;

   38 import android.app.ApplicationErrorReport;

   39 import android.app.Dialog;

   40 import android.app.IActivityController;

   41 import android.app.IActivityWatcher;

   42 import android.app.IApplicationThread;

   43 import android.app.IInstrumentationWatcher;

   44 import android.app.INotificationManager;

   45 import android.app.IProcessObserver;

   46 import android.app.IServiceConnection;

   47 import android.app.IThumbnailReceiver;

   48 import android.app.Instrumentation;

   49 import android.app.Notification;

   50 import android.app.NotificationManager;

   51 import android.app.PendingIntent;

   52import android.app.Service;

   53 import android.app.backup.IBackupManager;

   54 import android.content.ActivityNotFoundException;

   55 import android.content.BroadcastReceiver;

   56 import android.content.ComponentCallbacks2;

   57 import android.content.ComponentName;

   58 import android.content.ContentResolver;

   59 import android.content.Context;

   60 import android.content.DialogInterface;

   61 import android.content.Intent;

   62 import android.content.IntentFilter;

   63 import android.content.IIntentReceiver;

   64 import android.content.IIntentSender;

   65 import android.content.IntentSender;

   66 import android.content.pm.ActivityInfo;

   67 import android.content.pm.ApplicationInfo;

   68 import android.content.pm.ConfigurationInfo;

   69 import android.content.pm.IPackageDataObserver;

   70 import android.content.pm.IPackageManager;

   71 import android.content.pm.InstrumentationInfo;

   72 import android.content.pm.PackageInfo;

   73 import android.content.pm.PackageManager;

   74 import android.content.pm.PathPermission;

   75 import android.content.pm.ProviderInfo;

   76 import android.content.pm.ResolveInfo;

   77 import android.content.pm.ServiceInfo;

   78 import android.content.pm.PackageManager.NameNotFoundException;

   79 import android.content.res.CompatibilityInfo;

   80 import android.content.res.Configuration;

   81 import android.graphics.Bitmap;

   82 import android.net.Proxy;

   83 import android.net.ProxyProperties;

   84 import android.net.Uri;

   85 import android.os.Binder;

   86 import android.os.Build;

   87 import android.os.Bundle;

   88 import android.os.Debug;

   89 import android.os.DropBoxManager;

   90 import android.os.Environment;

91 import android.os.FileObserver;

   92 import android.os.FileUtils;

   93 import android.os.Handler;

   94 import android.os.IBinder;

   95 import android.os.IPermissionController;

   96 import android.os.Looper;

   97 import android.os.Message;

   98 import android.os.Parcel;

   99 import android.os.ParcelFileDescriptor;

  100 import android.os.Process;

  101 import android.os.RemoteCallbackList;

  102 import android.os.RemoteException;

  103 import android.os.ServiceManager;

  104 import android.os.StrictMode;

  105 import android.os.SystemClock;

  106 import android.os.SystemProperties;

  107 import android.provider.Settings;

  108 import android.text.format.Time;

  109 import android.util.EventLog;

  110 import android.util.Pair;

  111 import android.util.Slog;

  112 import android.util.Log;

  113 import android.util.PrintWriterPrinter;

  114 import android.util.SparseArray;

  115 import android.util.TimeUtils;

  116 import android.view.Gravity;

  117 import android.view.LayoutInflater;

  118 import android.view.View;

  119 import android.view.WindowManager;

  120 import android.view.WindowManagerPolicy;

  121 

  122 import java.io.BufferedInputStream;

  123 import java.io.BufferedOutputStream;

  124 import java.io.BufferedReader;

  125 import java.io.DataInputStream;

  126 import java.io.DataOutputStream;

  127 import java.io.File;

  128 import java.io.FileDescriptor;

  129 import java.io.FileInputStream;

91 import android.os.FileObserver;

   92 import android.os.FileUtils;

   93 import android.os.Handler;

   94 import android.os.IBinder;

   95 import android.os.IPermissionController;

   96 import android.os.Looper;

   97 import android.os.Message;

   98 import android.os.Parcel;

   99 import android.os.ParcelFileDescriptor;

  100 import android.os.Process;

  101 import android.os.RemoteCallbackList;

  102 import android.os.RemoteException;

  103 import android.os.ServiceManager;

  104 import android.os.StrictMode;

  105 import android.os.SystemClock;

  106 import android.os.SystemProperties;

  107 import android.provider.Settings;

  108 import android.text.format.Time;

  109 import android.util.EventLog;

  110 import android.util.Pair;

  111 import android.util.Slog;

  112 import android.util.Log;

  113 import android.util.PrintWriterPrinter;

  114 import android.util.SparseArray;

  115 import android.util.TimeUtils;

  116 import android.view.Gravity;

  117 import android.view.LayoutInflater;

  118 import android.view.View;

  119 import android.view.WindowManager;

  120 import android.view.WindowManagerPolicy;

  121 

  122 import java.io.BufferedInputStream;

  123 import java.io.BufferedOutputStream;

  124 import java.io.BufferedReader;

  125 import java.io.DataInputStream;

  126 import java.io.DataOutputStream;

  127 import java.io.File;

  128 import java.io.FileDescriptor;

  129 import java.io.FileInputStream;

根据上面,就可以看出,此类在是server层下的,当然,也是属于framework层下的,上面导入来哪些类来实现呢,从导入的情况可以发现有电池状态实现(BatteryStatesImpl),R,进程状态(ProcessState),属性缓存(AttributeCache),意图接收(IntentResolver),进程哈系(ProcessMap),系统服务(SystemService),观察狗(WatchDog),Activity栈状态(ActivityState),窗口管理服务(WindowManagerService),卵化器(Zygote),Activity,Activity管理(ActivityManager),Activity管理本地类(ActivityManagerNative),Activity线程(ActivityThread),警告对话框(AlertDialog),App全局(AppGlobals),应用错误报告(ApplicationErrorReport),对话框(Dialog),Activity控制接口(IActivityController),Activity观察接口(IActivityWather),应用线程接口(IApplicationThread),仪器观察接口(IStrumentationObserver),通知管理接口(INotificationManager),进程关注接口(IProcessObserver),服务链接接口(IServerConnection),缩略图接收器接口(IThumbnailReceiver),仪器(Instrumentation),通知(Notification),通知管理(NotificationManager),有待意图(PindingINtent),服务(Service),备份管理接口(IBackupManager),Activity没有找到异常(ActivityNotFoundException),广播接收者(BroadcastReceiver),组建回调2(ComponentCallbacks2),组建名称(ComponentName),内容分解(ContentResolver),上下文(Context),对话框接口(DialogInterface),意图(Intent),意图过滤器(IntentFilter),意图接收接口(IIntentReceiver),意图发送接口(IIntentSender),意图发送(IntentSender),Activity信息(ActivityInfo),应用信息(ApplicationInfo),配置信息(ConfigurationInfo),包数据关注接口(IPackageDataObserver),包管理接口(IPackageManager),仪器信息(InstrmatationInfo),包信息(PackageInfo),包管理(PackageManager),路径权限(PathPermission),提供者信息(ProviderInfo),解析器信息(ResolveInfo),服务信息(ServiceInfo),名称没有发现异常(NameNotFoundException),兼容性信息(CompatibilityInfo),配置(Configuration),位图(Bitmap),代理(Proxy),代理属性(ProxyProperties),统一资源标志符(Uri),帮第器(binder),建立(Build),捆(Bundle),调试(Debug),下拉框管理器(DropBoxManager),环境变量(Environment),文件关注(FileObserver),文件单元(FileUtils),处理器(Handler),捆绑器接口(IBinder),权限控制接口(IPermissionController),论寻(Looper),消息(Message),邮包(Parcel),邮包文件描述(ParcelFileDescript),进程(Process),远程回调列表(RemoteCallBackList),远程异常(RemoteException),服务管理(ServiceManager),严格模式(StrictMode),SystemClock(系统时钟),系统属性(SystemProperties),设置(Settings),时间(Time),事件日志(EventLog),一对(Pair),系统日志(SLog),日志(Log),输入打印(PrintWriterPrinter),数组解析(ParseArray),时间单元(TImeUtils),重力(Gravity),布局填充(LayoutInfalter),视图(View),窗口管理(WindowManager),窗口管理策略(WindowManagerPolicy),输入流缓冲(BufferedInputStream),输出流缓冲(BufferedOutputStream),缓冲读取(BufferedReader),数据输入流(DataInputStream),数据输出流(DataOutputStream),文件(File),文件描述(FileDescriptor),文件输入流(FileInputStream),文件没有发现异常(FileNotFoundException),文件输出流(FileOutputStream),IO异常(IOException),读取输入流(InputStreamReader),输入打印(PrintWriter),字符串写入(StringWriter),非状态异常(IllegalStateException),弱引用(WeakReference),数组列表(ArrayList),集合(Collections),比较(Comparator),哈系映射(HashMap),哈系集合(HashSet),迭代(Iterator),列表(List),语言环境(Locale),映射(Map),集合(Set),原子布尔(AtomicBoolean),原子长(AtimicLong).

OK,以上就是需要导入的相关类,可能有的人看到这里就有点晕了,其实际你看到这里并能很清晰的知道相干类的话,那说明你真的很懂了,毕竟这个类是一个重量级的类,容量达到了608k,代码标准行数达到来14382行,看看上面导入的类就知道这个类不可忽视,好来我们知道了上面,现在来具体分析下它的内部运行原理吧,在这里需要注意:

public class ActivityManagerService extends ActivityManagerNative implements Watchdog.Monitor,BatteryStatesImpl.BatteryCallBack{},这里它继承了本地窗口管理类,并且同时实现了观察狗的监听器与电池状态的实现回调,接下来,看首先声明的一些相关变量:

static final String TAG = “ActivityManager”;

  154     static final boolean DEBUG = false;

  155     static final boolean localLOGV = DEBUG;

  156     static final boolean DEBUG_SWITCH = localLOGV || false;

  157     static final boolean DEBUG_TASKS = localLOGV || false;

  158     static final boolean DEBUG_PAUSE = localLOGV || false;

  159     static final boolean DEBUG_OOM_ADJ = localLOGV || false;

  160     static final boolean DEBUG_TRANSITION = localLOGV || false;

  161     static final boolean DEBUG_BROADCAST = localLOGV || false;

  162     static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false;

  163     static final boolean DEBUG_SERVICE = localLOGV || false;

  164     static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false;

  165     static final boolean DEBUG_VISBILITY = localLOGV || false;

  166     static final boolean DEBUG_PROCESSES = localLOGV || false;

  167     static final boolean DEBUG_PROVIDER = localLOGV || false;

  168     static final boolean DEBUG_URI_PERMISSION = localLOGV || false;

  169     static final boolean DEBUG_USER_LEAVING = localLOGV || false;

  170     static final boolean DEBUG_RESULTS = localLOGV || false;

  171     static final boolean DEBUG_BACKUP = localLOGV || false;

  172     static final boolean DEBUG_CONFIGURATION = localLOGV || false;

  173     static final boolean DEBUG_POWER = localLOGV || false;

  174     static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false;

  175     static final boolean VALIDATE_TOKENS = false;

  176     static final boolean SHOW_ACTIVITY_START_TIME = true;

  177 

  178     // Control over CPU and battery monitoring.

  179     static final long BATTERY_STATS_TIME = 30*60*1000;      // write battery stats every 30 minutes.

  180     static final boolean MONITOR_CPU_USAGE = true;

  181     static final long MONITOR_CPU_MIN_TIME = 5*1000;        // don’t sample cpu less than every 5 seconds.

  182     static final long MONITOR_CPU_MAX_TIME = 0x0fffffff;    // wait possibly forever for next cpu sample.

  183     static final boolean MONITOR_THREAD_CPU_USAGE = false;

// The flags that are set for all calls we make to the package manager.

  186     static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES;

  187 

  188     private static final String SYSTEM_DEBUGGABLE = “ro.debuggable”;

  189 

  190     static final boolean IS_USER_BUILD = “user”.equals(Build.TYPE);

  191 

  192     // Maximum number of recent tasks that we can remember.

  193     static final int MAX_RECENT_TASKS = 20;

  194 

  195     // Amount of time after a call to stopAppSwitches() during which we will

  196     // prevent further untrusted switches from happening.

  197     static final long APP_SWITCH_DELAY_TIME = 5*1000;

  198 

  199     // How long we wait for a launched process to attach to the activity manager

  200     // before we decide it’s never going to come up for real.

  201     static final int PROC_START_TIMEOUT = 10*1000;

  202 

  203     // How long we wait for a launched process to attach to the activity manager

  204     // before we decide it’s never going to come up for real, when the process was

  205     // started with a wrapper for instrumentation (such as Valgrind) because it

  206     // could take much longer than usual.

  207     static final int PROC_START_TIMEOUT_WITH_WRAPPER = 300*1000;

  208 

  209     // How long to wait after going idle before forcing apps to GC.

  210     static final int GC_TIMEOUT = 5*1000;

  211 

  212     // The minimum amount of time between successive GC requests for a process.

  213     static final int GC_MIN_INTERVAL = 60*1000;

  214 

  215     // The rate at which we check for apps using excessive power — 15 mins.

  216     static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000;

  217 

  218     // The minimum sample duration we will allow before deciding we have

  219     // enough data on wake locks to start killing things.

  220     static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;

// The minimum sample duration we will allow before deciding we have

  223     // enough data on CPU usage to start killing things.

  224     static final int CPU_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;

  225 

  226     // How long we allow a receiver to run before giving up on it.

  227     static final int BROADCAST_TIMEOUT = 10*1000;

  228 

  229     // How long we wait for a service to finish executing.

  230     static final int SERVICE_TIMEOUT = 20*1000;

  231 

  232     // How long a service needs to be running until restarting its process

  233     // is no longer considered to be a relaunch of the service.

  234     static final int SERVICE_RESTART_DURATION = 5*1000;

  235 

  236     // How long a service needs to be running until it will start back at

  237     // SERVICE_RESTART_DURATION after being killed.

  238     static final int SERVICE_RESET_RUN_DURATION = 60*1000;

  239 

  240     // Multiplying factor to increase restart duration time by, for each time

  241     // a service is killed before it has run for SERVICE_RESET_RUN_DURATION.

  242     static final int SERVICE_RESTART_DURATION_FACTOR = 4;

  243 

  244     // The minimum amount of time between restarting services that we allow.

  245     // That is, when multiple services are restarting, we won’t allow each

  246     // to restart less than this amount of time from the last one.

  247     static final int SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000;

  248 

  249     // Maximum amount of time for there to be no activity on a service before

  250     // we consider it non-essential and allow its process to go on the

  251     // LRU background list.

  252     static final int MAX_SERVICE_INACTIVITY = 30*60*1000;

  253 

  254     // How long we wait until we timeout on key dispatching.

  255     static final int KEY_DISPATCHING_TIMEOUT = 5*1000;

// How long we wait until we timeout on key dispatching during instrumentation.

  258     static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000;

  259 

  260     static final int MY_PID = Process.myPid();

  261 

  262     static final String[] EMPTY_STRING_ARRAY = new String[0];

  263 

  264     public ActivityStack mMainStack;

上面是相关的一些前奏变量声明,看声明良不小哦,毕竟ActivityManagerService为Android内核的三大核心功能之一,Android内核的三大核心功能分别是:ActivityManagerService,WindowManagerService,View. 我从功能的角度可以知道ActivityManagerService主要有三大功能,即对应用程序的Activity作统一调度,内存管理与进程管理这三大主要功能,我们分析下,应用程序需要运行Activity,首先会通过ActivityManagerService来决定是否启动该Activity,再就是Activity退出后,所在的进程并不会立即被杀死,而会短时间存活在内存里,以便在下次启动时来提升其速度,当然在内存紧张时会对其杀死,这是由ActivityManagerService来完成的,应用程序并不需要对其管理,再就是进程管理,对其进程运行的API信息进行查询。

下面我们就单一来讲下,Activity调度:

思路:对其应用要启动的Activity必须通过ActivityManagerService来对记录的指示启动,当遇到其Activity停止或者启动需求时,它都会对其记录进行更新,从而掌握了Activity的启动与否的状态信息,在这里顺便来举一个Activity启动的几种方式:一般有四种:1.可在应用程序中调用this.startActivity()来启动指定的Activity,2.在Home程序中单击一个图标来启动新的Activity,3.按”Back(回退键)”来结束当前Activity,从而自动启动上一个Activity,4.长按Home键,显示出当前列表,从中选择一个启动。从上面可知,启动可分为顺序启动与前端处理启动,后面三种分为前端处理启动,看来要讲的还很多,今天就讲到这里吧,明天继续。。。

    原文作者:移动应用开发
    原文地址: http://www.cnblogs.com/phonegap/archive/2012/06/05/2535693.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞