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 were able to take a screenshot by holding both volume buttons + 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 if he is constantly working in the background or something built into every application or. Also, where is it in the Android source code?

Thanks!

+7
source share
4 answers

An Android app uses intentions to invoke system level resources. For example, most of the software to run the camera is provided by the OS. Android OS is based on the unix / linux kernel, so using the camera will look like this.

  • The application / process decides that it wants to use the camera.
  • This application / process creates the intent and transfers it to the OS.
  • The target is processed by the OS, and the camera software is executed (exec) in the new process. The calling process can either block it if it invokes the intent synchronously, or it can run asynchronously, and both the calling application and the application called by the intent can start simultaneously. In the camera example, it is most likely called synchronously and blocked.
  • The camera process is used by the user to take pictures.
  • The camera process terminates and creates either an error or an image.
  • The OS does some ipc (inter process communication) to transfer the image back to the calling process. The choice of using pipes or messaging remains in the OS based on the type of intent invoked by the application. The camera will probably use messaging because the parent process is blocked while the child process is running.
  • Now the calling process, which blocked the wait for the intention to return, wakes up the OS and returns to working state.
  • The image was returned to the calling application so that it could be used at its discretion.

When you take a screenshot, this is exactly what happens. The user interface processing is in progress. It basically sits and waits for input and filters everything that the OS should interpret before it is transferred to the currently running application. This is another unix convention, which is currently standard on every OS I can think of - streams with streams. This allows the OS to capture higher priority input, such as OS control commands, before they are passed to applications. (Think of ctrl-alt-delete in windows or ctrl-c / ctrl-z in unix.) When the screen keys are pressed, this upstream process launches a special program for taking the screen (using intent) and the command is never passed to the "running "application. This program executes in a new process, builds a raster image of the screen, saves it in the gallery space and dies - all asynchronously with the time manager's time distribution, so that the phone feels “responsive” all the time. You see in John Bokers an answer to the source code with all the gorp to implement this.

+1
source

Looking through the source code , I found a section that listens for the key combination and starts the process:

Here is the specific code:

http://androidxref.com/4.1.1/xref/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java#3270

+9
source

If you just want to take a screenshot very directly:

public Bitmap screenShot(View view) { Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; } 

just call this method with the view in which you want to take a picture .. so if you want the whole screen to simply go to the top of the ViewGroup . if you want the system controls to also call:

 screenShot((ViewGroup) view.getParent()); 

Now, if you are interested in how the volume + power keys work, deeper in the OS than you will have access too. Unable to intercept the power button from APK AFAIK.

+6
source

I found the code in the source tree here: https://github.com/android/platform_frameworks_base/tree/master/packages/SystemUI/src/com/android/systemui/screenshot

I think it’s just a matter of reading through it in order to better understand the process.

+6
source

All Articles