Frameworks/base/packages/systemui/src/com/android/systemui/screenshot/takescreenshotservice.java
Takescreenshotservice.java
PackageCom.android.systemui.screenshot;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.Handler;ImportAndroid.os.IBinder;ImportAndroid.os.Message;ImportAndroid.os.Messenger;Importandroid.os.RemoteException;ImportAndroid.os.UserManager;ImportAndroid.util.Log;ImportAndroid.view.WindowManager; Public classTakescreenshotserviceextendsService {Private Static FinalString TAG = "Takescreenshotservice"; Private StaticGlobalscreenshot Mscreenshot; PrivateHandler Mhandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {FinalMessenger callback =Msg.replyto; Runnable Finisher=NewRunnable () {@Override Public voidrun () {Message reply= Message.obtain (NULL, 1); Try{callback.send (reply); } Catch(RemoteException e) {}}; //If The storage for this user are locked, we have no place to store//the screenshot, so skip taking it instead of showing a misleading//animation and error notification. if(!getsystemservice (Usermanager.class). isuserunlocked ()) {LOG.W (TAG,"Skipping screenshot because storage is locked!"); Post (finisher); return; } if(Mscreenshot = =NULL) {Mscreenshot=NewGlobalscreenshot (Takescreenshotservice. This); } Switch(msg.what) { CaseWindowManager.TAKE_SCREENSHOT_FULLSCREEN:mScreenshot.takeScreenshot (finisher, Msg.arg1> 0, msg.arg2 > 0); Break; CaseWindowManager.TAKE_SCREENSHOT_SELECTED_REGION:mScreenshot.takeScreenshotPartial (Finisher, Msg.arg 1> 0, msg.arg2 > 0); Break; } } }; @Override Publicibinder onbind (Intent Intent) {return NewMessenger (Mhandler). Getbinder (); } @Override Public BooleanOnunbind (Intent Intent) {if(Mscreenshot! =NULL) Mscreenshot.stopscreenshot (); return true; }}
The main is to create a Globalscreenshot object, through the Globalscreenshot object to implement the screenshot, the reason that the class is hosted in the service because the screenshot may be time-consuming, this should be the design-time concept.
Of course, it is entirely possible to create Globalscreenshot objects by themselves, to achieve screenshots within the thread.
Android 7.1.1 System screenshot