I know HOW to take a screenshot, but I was wondering how it works.
At least since Android 4.0, you have been able to take a screen shot by holding down both the volume-down + power keys on the phone, and Android will capture the current screen and save it to the SD card.
I'm just wondering if anyone knows HOW this works, as in, if it's a service constantly running in the background, or something built into each app, or . Also, where is it located in the source code of Android?
Thanks!
The Android application use intents to invoke System level resources. For example -- most of the software to run the camera is provided by the OS. Android OS is based off of the unix/linux kernel so using the camera would look like this.
When you take a screen shot, this is exactly what happens. There is a process running to handle the user interface. It basically sits and waits for input and filters for anything that should be interpreted by the OS before it is piped to the current running application. This is another unix convention that at this point is standard in every OS I can think of -- piped input streams. It allows the OS to capture higher priority input, such as OS control commands before they are passed to applications. (think ctrl-alt-delete in windows, or ctrl-c/ctrl-z in unix.) When the screen shot keys are pressed, this upstream io process execs a special program to take a screen shot (via intent) and the command is never actually passed to the 'running' application. That program is executed (exec) in a new process, builds a bitmap of the screen, saves it to the gallery space, and dies -- all asynchronously with the scheduler handling time sharing so the phone feels 'responsive' the whole time. You see in John Bokers answer the source code with all the gorp to implement this.