The program has two functions, one is the eye lamp, the second is the flashlight, but the flashlight compatibility solution is to add abnormal control, to avoid a variety of abnormal occupation can not start the flashlight.
The main program interface, no landscaping, only to achieve basic functions.
Part of the code:
Protective Eye Mask Main code
private void CreateView () {Mwindowmanager = (WindowManager) getapplication (). Getsystemservice(Context. WINDOW_service);Wmparams = new WindowManager. Layoutparams();Phone window. It is used for telephone interactions (especially inbound). It is placed above all applications, under the status bar. Wmparams. Type= WindowManager. Layoutparams. TYPE_phone;Set the picture format, the effect is transparent background wmparams. Format= PixelFormat. RGBA_8888;Set the floating window to be non-focusable (operations that enable operation of other visible windows except floating windows) Wmparams. Flags= WindowManager. Layoutparams. FLAG_not_touch_modal | WindowManager. Layoutparams. FLAG_not_focusable | WindowManager. Layoutparams. FLAG_not_touchable;Adjust the hover window to display the docking position to the left of the pinned Wmparams. Gravity= Gravity. Left| Gravity. TOP;Wmparams. x=0;Wmparams. Y=0;Setting the hover window length-width data wmparams. Width= WindowManager. Layoutparams. MATCH_parent;Wmparams. Height= WindowManager. Layoutparams. MATCH_parent;v = new MyView (Getapplicationcontext ());Mwindowmanager. AddView(V, Wmparams);}
Flashlight Main Code
/** * Turn the flashlight on or off * * PrivateView.onclicklistener Flash =NewView.onclicklistener () {@Override Public void OnClick(View v) {//First to determine if there is a flash if(!getpackagemanager (). Hassystemfeature (Packagemanager.feature_camera_flash)) {Toast.maketext (mainactivity). This,"The current device does not have a flashing light", Toast.length_long). Show ();return; }if(flashstate) {Close (); Btnstartflash.settext ("Turn on the flashlight"); }Else if(!flashstate) {open (); Btnstartflash.settext ("Turn off the flashlight"); } } };Private void Initflashmode() {Try{camera = Camera.open (); Camera.setpreviewtexture (NewSurfacetexture (0)); }Catch(IOException e) {Close (); }if(Camera! =NULL) {parameters = Camera.getparameters (); Previousflashmode = Parameters.getflashmode (); }if(Previousflashmode = =NULL) {Previousflashmode = Camera.Parameters.FLASH_MODE_OFF; } }//Turn off the camera Private void Close() {if(camera!=NULL) {Parameters.setflashmode (Previousflashmode); Camera.setparameters (parameters); Camera.release (); Camera =NULL; Flashstate =false; } }//Open camera Private void Open(){if(camera==NULL) {Initflashmode (); }if(camera!=NULL) {Parameters.setflashmode (Camera.Parameters.FLASH_MODE_TORCH); Camera.setparameters (parameters); Camera.startpreview (); Flashstate =true; } }
Complete code can contact me [email protected]
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android---Eye-protection and high-compatibility flashlight implementation