Android integrates a large number of System Manager managers. For example, if you want to kill a background Processes, you will use ActivityManager. For example, if you want to use the system sound, you need AudioManager. It is very easy for you to get these housekeepers. For example, to get an ActivityManager, you only need to call the current context method getSystemService:
final ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE);
You often use the context method getSystemService to obtain the Manager you want. Do you know why any context instance can obtain the Manager? Today is another time, let's look at its secret.
First, we know that Context is a global resource and information that describes an application. Context is actually an abstract class, so it cannot be used directly. Its implementation class is actually ContextImpl. Let's take a look at the original article about ContextImpl in Android:
/** * Common implementation of Context API, which provides the base * context object for Activity and other application components. */
From the above, we can know that ContextImpl is actually the implementation of context, and the application Component actually inherits from it! Therefore, to study context, we need to study ContextImpl. It seems a little out of question. Hey, let's continue.
As mentioned earlier, the context method getSystemService can obtain various managers currently used by the android system. Let's take a look at what getSystemService is doing in ContextImpl.
@Override public Object getSystemService(String name) { ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name); return fetcher == null ? null : fetcher.getService(this); }
Here we will look at two points:
(1) first, let's see what SYSTEM_SERVICE_MAP is?
private static final HashMap
SYSTEM_SERVICE_MAP = new HashMap
();
It was originally a HashMap and a static private variable. This indicates that SYSTEM_SERVICE_MAP stores a public and shared Number of ServiceFetcher instances.
When will the ServiceFetcher values stored in SYSTEM_SERVICE_MAP be initialized? Let's talk about it later.
(2) Let's see What ServiceFetcher is?
/*** Main entrypoint; only override if you don't need caching. */public Object getService (ContextImpl ctx) {ArrayListCache = ctx. mServiceCache; Object service; synchronized (cache) {if (cache. size () = 0) {// Initialize the cache vector on first access. // At this point sNextPerContextServiceCacheIndex // is the number of potential services that are // cached per-Context. for (int I = 0; I <sNextPerContextServiceCacheIndex; I ++) {cache. add (null) ;}} else {service = cache. get (mContextCacheIndex); if (service! = Null) {return service ;}} service = createService (ctx); cache. set (mContextCacheIndex, service); return service ;}/ *** Override this to create a new per-Context instance of the * service. getService () will handle locking and caching. */public Object createService (ContextImpl ctx) {throw new RuntimeException ("Not implemented ");}}A simple static class, with only two methods getService and createService:
Let's take a look at the createService method: there is nothing in the change method. Let's take a look at the original description of this method: it is clear that this method is actually used for Override, the purpose is to create a Service.
Let's take a look at getService. We can know from this method: First, check whether the ctx. mServiceCache contains the Service we need. If not, call createService to create a Service.
Therefore, ServiceFetcher is actually an encapsulation class for Service creation and access for convenience.
So the key point is how to create a Service.
Soon, you will find a piece of static code in ContextImpl, which registers all the services:
static { registerService(ACCESSIBILITY_SERVICE, new ServiceFetcher() { public Object getService(ContextImpl ctx) { return AccessibilityManager.getInstance(ctx); }}); registerService(ACCOUNT_SERVICE, new ServiceFetcher() { public Object createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(ACCOUNT_SERVICE); IAccountManager service = IAccountManager.Stub.asInterface(b); return new AccountManager(ctx, service); }}); registerService(ACTIVITY_SERVICE, new ServiceFetcher() { public Object createService(ContextImpl ctx) { return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); }});...}
The createService method is used to create a new service object.
Let's finally look at what the registerService method has done?
Private static void registerService (String serviceName, ServiceFetcher fetcher ){
If (! (Fetcher instanceof StaticServiceFetcher )){
Fetcher. mContextCacheIndex = sNextPerContextServiceCacheIndex ++;
}
SYSTEM_SERVICE_MAP.put (serviceName, fetcher );
}
SYSTEM_SERVICE_MAP.put (serviceName, fetcher); have you seen it! In fact, the created ServiceFetcher is stored in the HashMap SYSTEM_SERVICE_MAP.
Finally, we can see all the Manager Services:
1. EntropyService
Entropy service, periodic loading and storage of random information. It is mainly because after linux is started, the/dev/random status may be predictable, so some applications that require random information may have problems. No application interface is required.
2. PowerManagerService-> PowerManager
Android power management is also an important part. For example, you can turn off unnecessary devices in the standby mode, the screen and keyboard Backlight in the standby mode, and the number of devices to be turned on during user operations.
3. ActivityManagerService-> ActivityManager
This is the most core service in the entire Android framework. It manages the core implementation of tasks, process management, and Intent parsing in the entire framework. Although the Manager Service is called Activity, it is not only subject to Activity, but also has three other components and processes. That is to say, the user is responsible for application lifecycle management.
4. TelephonyRegistry-> TelephonyManager
The telephone registration and management service module can obtain the telephone connection status, signal strength, and so on.
<可以删掉,但要看的大概明白>
5. PackageManagerService-> PackageManager
This includes unpacking, verification, installation, and upgrade of the software package. To solve the problem that we cannot install the. so file, we should first analyze the cause from this.
6. AccountManagerService-> AccountManager
A system service that provides account, password, and authtoken management for all
Accounts on the device.
7. ContentService-> ContentResolver
Content services are mainly services that provide solutions such as databases.
8. BatteryService
Monitors battery charging and status services. Intent is broadcast when the status changes.
9. HardwareService
Generally, it is the service program of ring and vibrate.
10. SensorService-> SensorManager
Manages the services of the Sensor device, registers the client device, and activates the sensor when the client needs to use the Sensor.
11. WindowManagerService-> WindowManager-> PhoneWindowManager
Highly bonded with ActivityManagerService
Window Management, the core here is the distribution and management of input events.
12. AlarmManagerService-> AlarmManager
Alarm service program
13. Define thservice-> define thdevice
Bluetooth background management and service programs
14. StatusBarService-> StatusBarManager
It is responsible for the statusBar Standard update, animation, and other services with little service.
15. ClipboardService-> ClipboardManager
Similar to the clipBoard service of other systems, it provides copy and paste capabilities.
16. InputMethodManagerService-> InputMethodManager
The Management Service Program of the input method, including when to enable the input method, and switching the input method.
17. NetStatService
Mobile Network Service
18. ConnectivityService-> ConnectivityManager
The network connection status service can be queried by other applications. When the network status changes, it can also be broadcast and changed.
19. AccessibilityManagerService-> AccessibilityManager
This may take a closer look. It mainly involves the distribution and management of click, focus, text changes, and other events, debugging and problem locating of the entire system, you also need to carefully view this service.
20. icationicationmanagerservice-> icationicationmanager
Responsible for managing and notifying the occurrence of background events. This is glued together with statusbar, and the response icon is usually added to statusbar. You can use this to know what happened in the system background.
21. MountService
A service program for disk loading usually works with a linux daemon program, such as vold/mountd, to listen for and broadcast device mount/unmount/bad removal events.
22. DeviceStorageMonitorService
Monitors disk space services. If the disk space is less than 10%, a warning is given to the user.
23. LocationManagerService-> LocationManager
To join the GPS service, take a closer look at this part. Now the navigation in the application does not respond. You can start to look at it.
24. SearchManagerService-> SearchManager
The search manager service handles the search UI, and maintains a registry of searchable activities.
25. Checkin Service (FallbackCheckinService)
It seems that checkin service is a package provided by google, with no source code. The source code is fallbackCheckinService.
26. WallpaperManagerService-> WallpaperManager
Services that manage the desktop background and deeply customize the desktop system, which must be understood and extended
<同时要兼容>
This part
27. AudioService-> AudioManager
The upper-Layer Management encapsulation of AudioFlinger mainly manages volume, sound effects, sound channels, and ringtones.
28. HeadsetObserver
Monitoring cycle of earphone plugging events
29. DockObserver
If there is a license in the system, when the mobile phone is installed or pulled out, it will have to be managed by him.
30. BackupManagerService-> BackupManager
Backup Service
31. AppWidgetService-> AppWidgetManager
Android allows users to write programs on the desktop as Widgets. This is the interface for this process and service.