Android Internals
Android Internals
PowerManager pm = (PowerManager)
getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock =
pm.newWakeLock(PowerManager.FULL_WAKE_LOCK,
"myPreciousWakeLock");
wakeLock.acquire(100);
Service Manager and Binder Interaction
- Think of the Service Manager as a Yellow Pages book of
all services available in the system. If a system service
isn’t registered with the Service Manager, then it’s
effectively invisible to the rest of the system.
- To provide this indexing capability, the Service Man‐
ager is started by init before any other service. It then
opens /dev/binder and uses a special ioctl() call to set
itself as the Binder’s Context Manager(A1).
- Thereafter, any process in the system that attempts to
communicate with Binder ID 0 is actually communicating
through Binder to the Service Manager.
Thereafter, any process in the system that attempts
to communicate with Binder ID 0
Service Manager and Binder Interaction
- When the System Server starts, for instance, it registers every
single service it instantiates with the Service Manager (A2).
- when an app tries to talk to a system service, such as the Power
Manager service, it first asks the Service Manager for a handle to
the service (B1) and then invokes that service’s methods (B2).
- a call to a service component running within an app goes directly
through Binder (C1) and is not looked up through the Service
Manager.
- The Service Manager is also used in a special way by a number of
command-line utilities such as the dumpsys utility, which allows you
to dump the status of a single or all system
Services
- To get the list of all services, dumpsys loops around to get every
system service (D1)
- each service, dumpsys just asks the Service Manager to locate that
specific one (D2).
- With a service handle in hand, dumpsys invokes that service’s dump()
function to dumpits status (D3) and displays that on the terminal.
Then Activity Manager
- It takes care of the starting of new components, such as
Activities and Services, along with the fetching of Content
Providers and intent broadcasting. If you ever got the
dreaded ANR (Application Not Responding) dialog box,
know that the Activity Manager was behind it.
- It’s also involved in the maintenance of OOM adjustments
used by the in-kernel low-memory handler, permissions,
task management, etc
- when the user clicks an icon to start an app from his home
screen, the first thing that happens is the Launcher’s
onClick() callback is called (the Launcher being the default
app packaged with the AOSP that takes care of the main
interface with the user, the home screen). To deal with
the event, the Launcher will then call, through Binder, the
startActivity() method of the Activity Manager service.
The service will then call the startViaZygote() method,
which will open a socket to the Zygote and ask it to start
the Activity.
System Startup
- Steps
- It has a hardcoded address from which it fetches its first
instructions. That address usually points to a chip that has
the bootloader programmed on it.
- The bootloader then initializes the RAM, puts basic hardware
in a quiescent state, loads the kernel and RAM disk, and jumps
into the kernel.
- The Zygote is a special daemon whose job is to launch
apps.The init doesn’t actually start the Zygote directly;
instead it uses the app_process command to get Zygote
started by the Android Runtime.The runtime then
starts the first Dalvik VM of the system and tells it to invoke
the Zygote’s main() .
Zygote is active only when a new app needs to be launched.The
Zygote then listens for connections on its socket
(/dev/socket/zygote) for requests to start new apps. When it
gets a request to start an app, it forks itself and launches the
new app.
12
10
Column 1
6
Column 2
Column 3
0
Row 1 Row 2 Row 3 Row 4