Capture of a complete Android source screenshot event in Android 4.3

Source: Internet
Author: User
Tags final

1. Background

We know that Android provides a system screen-cutting function, which is to hold down the power button and the volume minus button for 0.5 seconds, and the system will perform a screen-cutting function. So to achieve the function of the system screen, is to capture the system of the two key combination of the functions below, and then a layer of digging down. The version found online now is the screenshot () function under the Surface.java file, which is @hide. But this is a previous version of the approach that is not applicable after android4.3, because there is no screenshot () function under/frameworks/base/core/java/android/view/Surface.java, I guess Google will not be so unfeeling, it will be in the framework layer to the developer to leave an interface, only to write somewhere else. So bloggers follow the ideas of their predecessors, and finally found a new version of the screenshot function, in this share with you.

2. Excavation process

(1) The capture of keys in the Android source code is located in file Phonewindowmanager.java (\frameworks\base\policy\src\com\android\internal\policy\impl)

ASE keyevent.keycode_volume_up: Case  
            keyevent.keycode_volume_mute: {  
                if (keycode = = Keyevent.keycode_volume_ Down) {  
                    if (down) {  
                        if (isscreenon &&!mvolumedownkeytriggered  
                                && (Event.getflags () & keyevent.flag_fallback) = = 0) {  
                            mvolumedownkeytriggered = true;  
                            Mvolumedownkeytime = Event.getdowntime ();  
                            Mvolumedownkeyconsumedbyscreenshotchord = false;  
                            Cancelpendingpowerkeyaction ();  
                            Interceptscreenshotchord ();  
                        }  
                    else {  
                        mvolumedownkeytriggered = false;  
                        Cancelpendingscreenshotchordaction ();  
                    }

We see that if the power button is pressed at the same time and the volume reduction starts the function,

Interceptscreenshotchord ();

Let's see a function.

private void Interceptscreenshotchord () {  
        if (mvolumedownkeytriggered && mpowerkeytriggered &&!) mvolumeupkeytriggered) {  
            final long now = Systemclock.uptimemillis ();  
            if (now <= mvolumedownkeytime + screenshot_chord_debounce_delay_millis  
                    && now <= Mpowerkeytime + Screenshot_chord_debounce_delay_millis) {  
                Mvolumedownkeyconsumedbyscreenshotchord = true;  
                Cancelpendingpowerkeyaction ();  
      
                Mhandler.postdelayed (mscreenshotchordlongpress,  
                        viewconfiguration.getglobalactionkeytimeout ());}}}  
    

We see this function in handle, which is what we're going to do with the screen-cutting function.

Mscreenshotchordlongpress

We enter this function

Private final Runnable mscreenshotchordlongpress = new Runnable () {public  
        void run () {  
            takescreenshot ();  
        }  
    };

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

Finally let us find takescreenshot, but don't worry, we go to this function

private void Takescreenshot () {synchronized (Mscreenshotlock) {if (mscreenshotconnection!= nul  
            L) {return; } componentname cn = new ComponentName ("Com.android.systemui", "Com.android.systemui.s Creenshot.  
            Takescreenshotservice ");  
            Intent Intent = new Intent ();  
            Intent.setcomponent (CN); Serviceconnection conn = new Serviceconnection () {@Override public void Onserviceconnec  
                        Ted (componentname name, IBinder service) {synchronized (Mscreenshotlock) {  
                        if (mscreenshotconnection!= this) {return;  
                        Messenger Messenger = new Messenger (service);  
                        Message msg = Message.obtain (null, 1);  
                        Final Serviceconnection myconn = this; HaNdler h = new Handler (Mhandler.getlooper ()) {@Override Public V  
                                    OID handlemessage (Message msg) {synchronized (Mscreenshotlock) { if (mscreenshotconnection = = myconn) {Mcontext.unbindservice (MS  
                                        Creenshotconnection);  
                                        Mscreenshotconnection = null;  
                                    Mhandler.removecallbacks (mscreenshottimeout);  
                        }  
                                }  
                            }  
                        };  
                        Msg.replyto = new Messenger (h);  
                        MSG.ARG1 = MSG.ARG2 = 0;  
                        if (Mstatusbar!= null && mstatusbar.isvisiblelw ()) msg.arg1 = 1; if (Mnavigationbar!= null && mnavigationbAR.ISVISIBLELW ()) msg.arg2 = 1;  
                        try {messenger.send (msg); The catch (RemoteException e) {}}} @O  
            Verride public void onservicedisconnected (componentname name) {}};  
                if (Mcontext.bindservice (intent, Conn, context.bind_auto_create)) {mscreenshotconnection = conn;  
            Mhandler.postdelayed (Mscreenshottimeout, 10000); }  
        }  
    }

We saw that it started a screenshot of the service ("Com.android.systemui.screenshot.TakeScreenshotService"). (It also confirms my guess that all of Android's functions are handle through SendMessage and then through service)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.