Android-screen recording Technology Using Reflection

Source: Internet
Author: User

Screenshotservice. Java

Package COM. screenshot. ui; import android. annotation. suppresslint; import android. app. service; import android. content. intent; import android. graphics. bitmap; import android. graphics. bitmap. config; import android. graphics. canvas; import android. graphics. paint; import android. OS. environment; import android. OS. handler; import android. OS. ibinder; import android. OS. message; import android. view. view; import Java. io. f Ile; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. lang. reflect. field; public class screenshotservice extends Service {public static final Boolean sss_default_start = false; public static final Boolean sss_start = true; public static final Boolean sss_stop = false; public static final string transcribe_tag = "transcribe_tag"; public static fi NAL long sss_delay_millis = 100l; public static final string sss_file_path = "/luzhi/"; public static final int sss_msg_what = 256; private screenshotservicehandler handler; private Boolean isstart = sss_stop; private view [] getwindowdecorviews () throws exception {class <?> Windowmanager = Class. forname ("android. view. windowmanagerimpl "); field viewsfield = windowmanager. getdeclaredfield ("mviews"); field instancefield = windowmanager. getdeclaredfield ("mwindowmanager"); viewsfield. setaccessible (true); instancefield. setaccessible (true); object instance = instancefield. get (null); view [] viewarray = (view []) viewsfield. get (instance); If (viewarray = NULL) | (viewarray. length <0) {return NULL;} int I = 0; view [] views = new view [viewarray. length]; for (view V: viewarray) {views [I ++] = V;} int [] arrayofint = new int [2]; view localview; Int J = 0; int length = views. length; for (I = 0; I <length; I ++) {localview = views [I]; localview. getlocationonscreen (arrayofint); If (arrayofint [0]> 0) | (arrayofint [1]> 0) {for (j = I + 1; j <views. length; j ++) {views [J-1] = views [J] ;} Views [views. length-1] = localview; Length --; I --;} return views;} private void savepic (Bitmap parambitmap, string paramstring) {fileoutputstream Fos = NULL; try {Fos = new fileoutputstream (paramstring); If (null! = FOS) {parambitmap. compress (bitmap. compressformat. PNG, 90, FOS); FOS. flush (); FOS. close () ;}} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}} private void screenshot () {try {view [] arrayofview = getwindowdecorviews (); bitmap localbitmap = view2bitmap (arrayofview); New savefilethread (localbitmap ). start ();} catch (exception localexception) {localti Ti On. printstacktrace () ;}} private bitmap view2bitmap (view paramview) {log. E ("savefilethread view2bitmap (view)"); paramview. setdrawingcacheenabled (true); paramview. builddrawingcache (); bitmap localbitmap1 = paramview. getdrawingcache (); bitmap localbitmap2 = NULL; If (localbitmap1! = NULL) {localbitmap2 = bitmap. createbitmap (localbitmap1);} paramview. destroydrawingcache (); Return localbitmap2;} private bitmap view2bitmap (view [] paramarrayofview) {canvas localcanvas = NULL; paint localpaint = NULL; bitmap newbitmap = NULL; int [] arrayofint = new int [2]; for (view localview: paramarrayofview) {bitmap localbitmap = view2bitmap (localview); localview. getlocationonscreen (arrayofint); If (L Ocalbitmap! = NULL) {If (localcanvas = NULL) {newbitmap = bitmap. createbitmap (localview. getwidth (), localview. getheight (), config. argb_8888); // create a new and srclocalcanvas = new canvas (newbitmap); localpaint = new paint ();} localcanvas. drawbitmap (localbitmap, arrayofint [0], arrayofint [1], localpaint); localbitmap. recycle (); localbitmap = NULL;} If (localcanvas! = NULL) {localcanvas. save (canvas. all_save_flag); // save localcanvas. restore (); // store} return newbitmap;} public ibinder onbind (intent paramintent) {return NULL;} public void oncreate () {super. oncreate (); log. E ("screenshotservice oncreate"); handler = new screenshotservicehandler ();} public void onstart (intent paramintent, int paramint) {super. onstart (paramintent, paramint); log. E ("screenshotservice onstart -- --- Start ---- "); string STR = transcribe_tag; Boolean localstart = paramintent. getbooleanextra (STR, sss_default_start); If (this. isstart! = Localstart) {This. isstart = localstart; If (this. isstart = sss_start) {handler. sendemptymessagedelayed (sss_msg_what, sss_delay_millis);} else {handler. removemessages (sss_msg_what) ;}}@ suppresslint ({"handlerleak", "handlerleak"}) private class screenshotservicehandler extends handler {public void handlemessage (Message parammessage) {If (parammessage. what = sss_msg_what) {screenshotservice. this. s Creenshot (); If (screenshotservice. this. isstart) {screenshotservice. this. handler. sendemptymessagedelayed (sss_msg_what, sss_delay_millis) ;}}} private class savefilethread extends thread {private bitmap mbitmap; Public savefilethread (Bitmap arg2) {This. mbitmap = arg2;} public void run () {If (this. mbitmap = NULL) {log. E ("savefilethread run mbitmap is null");} file localfile1 = environment. getexternalst Oragedirectory (); file localfile2 = new file (localfile1, sss_file_path); If (! Localfile2.exists () {localfile2.mkdirs ();} string filename = localfile2.getabsolutepath () + "/" + system. currenttimemillis () + ". PNG "; log. E ("savefilethread run picpath =" + filename); screenshotservice. this. savepic (mbitmap, filename); this. mbitmap. recycle (); this. mbitmap = NULL ;}}}

Playactivity. Java

Package COM. screenshot. ui; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. OS. bundle; import android. OS. environment; import android. OS. handler; import android. OS. message; import android. view. window; import android. view. windowmanager; import android. widget. imageview; import Java. io. file; import Java. util. arrays; import Java. util. comparator; import Com. screenshot. r; public class playactivity extends activity {private playactivityhandler mhandler; private imageview play_iv; private file [] arrayoffile2; private file [] getfilelist () {file localfile1 = environment. getexternalstoragedirectory (); file localfile2 = new file (localfile1, screenshotservice. sss_file_path); file [] arrayoffile; If (! Localfile2.exists () {return NULL;} arrayoffile = localfile2.listfiles (); arrays. sort (arrayoffile, new comparator <File> () {@ overridepublic int compare (File paramfile1, file paramfile2) {long file1name = long. valueof (getfilenamenoex (paramfile1.getname (); long file2name = long. valueof (getfilenamenoex (paramfile2.getname (); Return (INT) (file1name-file2name) ;}}); Return arrayoffile;} private void play () {Arrayoffile2 = getfilelist (); mhandler. setcount (0); mhandler. sendemptymessagedelayed (screenshotservice. sss_msg_what, screenshotservice. sss_delay_millis);} Public String getextensionname (string paramstring) {If (paramstring! = NULL) & (paramstring. length ()> 0) {int I = paramstring. lastindexof ('. '); if (I>-1) {Int J = paramstring. length () +-1; if (I <j) {int K = I + 1; paramstring = paramstring. substring (k) ;}}return paramstring;} Public String getfilenamenoex (string paramstring) {If (paramstring! = NULL) & (paramstring. length ()> 0) {int I = paramstring. lastindexof ('. '); if (I>-1) {Int J = paramstring. length (); if (I <j) paramstring = paramstring. substring (0, I) ;}} return paramstring;} protected void oncreate (bundle parambundle) {super. oncreate (parambundle); // set the Untitled requestwindowfeature (window. feature_no_title); // sets full screen getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmana Ger. layoutparams. flag_fullscreen); setcontentview (R. layout. play); this. play_iv = (imageview) findviewbyid (R. id. play_iv); mhandler = new playactivityhandler (); play ();} private class playactivityhandler extends handler {private int COUNT = 0; // Private bitmap localbitmap; Public void setcount (INT vaule) {COUNT = vaule ;}@ overridepublic void handlemessage (Message MSG) {If (arrayoffile2! = NULL & count <arrayoffile2.length) {bitmap localbitmap = bitmapfactory. decodefile (arrayoffile2 [count ++]. getabsolutepath (); play_iv.setimagebitmap (localbitmap); mhandler. sendemptymessagedelayed (screenshotservice. sss_msg_what, screenshotservice. sss_delay_millis );}}}}
Related Article

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