Android4_src _ analysis ~ Activitymanagerservice ~ Illustration (1)

Source: Internet
Author: User

Okay. From now on, we will give a key point to make a detailed analysis of the knowledge points in the current development comparison of Android from the source code. Of course, here, I mainly analyze the code at the framework layer. Today I will analyze the code about activitymanagerservice. java class, the content of analysis is mainly based on its internal principle. Because there are many classes of code, it is impossible for me to analyze and present them all in one space, so I mainly analyze its internal principles:
Let's take a look at the following:
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. ithumbnailpolicer;
48 import Android. App. instrumentation;
49 import Android. App. notification;
50 import Android. App. icationicationmanager;
51 import Android. App. pendingintent;
52 import 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. logoff;
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. logoff;
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;
According to the above, we can see that this class is at the server layer. Of course, it also belongs to the framework layer. which classes are imported above for implementation, from the import, you can find batterystatesimpl, R, processstate, attributecache, and intentresolver ), processmap, systemservice, watchdog, activitystate, windowmanagerservice, and zygote ), activity, activity management (activitymanager), activity management local class (activitymanagernative), activity thread (activitythread), warning dialog box (alertdialog), app global (appglobals ), applicationerrorreport, dialog box (DIALOG), activity control interface (iactivitycontroller), activity observation interface (iactivitywather), Application Thread interface (iapplicationthread), and instrument observation interface (istrumentationobserver ), inoicationicationmanager, iprocessobserver, iserverconnection, ithumbnailreceiver, instruments, and notification ), notification manager, pindingintent, service, ibackupmanager, activitynotfoundexception, broadcastreceiver ), componentcallbacks2, componentname, contentresolver, context, dialoginterface, and intent ), intentfilter, iintentsender, intentsender, activityinfo, and applicationinfo ), configurationinfo, ipackagedataobserver, ipackagemanager, instruments info, packageinfo, and packagemanager ), pathpermission, providerinfo, resolveinfo, serviceinfo, namenotfoundexception, compatibilityinfo ), configuration, bitmap, proxy, proxyproperties, Uniform Resource Identifier (URI), binder ), build, bundle, debug, dropboxmanager, environment, fileobserver, and fileutils ), processor (handler), bundle interface (ibinder), permission Control Interface (ipermissioncontroller), logoff, message, and parcel ), parcelfiledescript, process, remotecallbacklist, RemoteException, servicemanager, and strictmode ), systemclock, systemproperties, settings, time, Eventlog, pair, system log ), log, printwriterprinter, parsearray, timeutils, gravity, layoutinfalter, View ), windowmanager, bufferedinputstream, bufferedoutputstream, bufferedreader, and datainputstream ), dataoutputstream, file, filedescriptor, fileinputstream, filenotfoundexception, and fileoutputstream ), io exception, inputstreamreader, printwriter, stringwriter, illegalstateexception, and weakreference ), array list, collections, comparator, hashmap, hashset, iterator, list ), locale, MAP, set, atomicboolean, and atimiclong ).
OK. The above is the relevant class to be imported. Some people may be dizzy when they see it. If you actually see it and can clearly understand it, that means you really understand. After all, this class is a heavyweight class with a capacity of 608 KB and a code standard line number of 14382, let's take a look at the imported class and we will know that this class should not be ignored. Let's take a look at the above. Now let's take a look at its internal operating principles. here we need to note:
Public class activitymanagerservice extends activitymanagernative implements watchdog. monitor, batterystatesimpl. batterycallback {}, which inherits the local window management class and implements callback for observing the dog's listener and battery status. Next, let's take a look at the declared variables:
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 callwe 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 stopovers witches () 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 // cocould 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 runner er 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
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
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;
The above are some of the relevant prelude variable declarations. Obviously, activitymanagerservice is one of the three core functions of the android kernel. The three core functions of the android kernel are activitymanagerservice and windowmanagerservice, view. from a functional perspective, I can understand that activitymanagerservice has three main functions: Unified scheduling of application activities, and memory management and process management. In our analysis, the application needs to run the activity. First, the activitymanagerservice is used to determine whether to start the activity. After the activity exits, the process in which it is located will not be killed immediately and will survive in the memory for a short time, so that the speed can be improved at the next startup, of course, it will be killed when the memory is tight, this is done by activitymanagerservice, the application does not need to manage it, and then the process To query the API information of the process.
Next, let's talk about activity scheduling:
Idea: The activity whose application is to be started must be started using activitymanagerservice to indicate the recording. When its activity is stopped or needs to be started, it will update its records, in this way, you can understand the status information of activity startup. Here, we can refer to the following methods for starting an activity: Generally, there are four types: 1. you can call this in an application. startactivity () to start the specified activity, 2. click an icon in the Home program to start a new activity. press "back (rollback key)" to end the current activity and start the previous activity automatically. long press the Home Key to display the current list and select a startup. As we can see from the above, the start can be divided into sequential start and front-end processing start, And the next three are frontend Processing start. It seems that there are still many things to talk about. Let's talk about it today and continue tomorrow...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.