Search code examples
androidscreenshot

How does Android take screenshots?


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!


Solution

  • 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.

    1. An Application/Process decides it wants to use the camera.
    2. That Application/Process builds an intent and passes it to the OS.
    3. The intent is processed by the OS, and the camera software is executed (exec) in a new process. The calling process can either block it if it called the intent synchronously, or it can run asynchronously and both the calling application and the intent invoked application can run at the same time. In the example of a camera, it will likely be called synchronously and block.
    4. The camera Process is used by the user to take a picture.
    5. The Camera process exits and produces either an error or a picture.
    6. The OS performs some ipc (inter process communication) to pass the picture back to the calling process. The choice to uses pipes or message passing is left to the OS, based on the kind of intent called by the application. A camera will probably use message passing since the parent process is blocked while the child process is running.
    7. Now the calling process, which was blocking waiting on the intent to return is awakened by the OS and returned to a running state.
    8. The Picture has been returned to the calling application, for it to use as it wishes.

    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.