Android --- eye lamp and high compatibility flashlight
The program has two main functions: Eye lamp and flashlight. However, the solution to the flashlight compatibility is to add exception control to avoid various abnormal occupation and startup failure.
The main interface of the program, without beautification, only implements basic functions.
Some code:
Main Code of Eye Mask
Private void CreateView () {mWindowManager = (WindowManager) getApplication (). getSystemService (Context. WINDOW_SERVICE); wmParams = new WindowManager. LayoutParams (); // phone window. It is used for telephone interaction (especially incoming calls ). It is placed above all applications and under the status bar. WmParams. type = WindowManager. layoutParams. TYPE_PHONE; // set the image format. The effect is the background transparency. format = PixelFormat. RGBA_8888; // set the floating window to not be focused (to perform operations on visible windows other than floating windows) wmParams. flags = WindowManager. layoutParams. FLAG_NOT_TOUCH_MODAL | WindowManager. layoutParams. FLAG_NOT_FOCUSABLE | WindowManager. layoutParams. FLAG_NOT_TOUCHABLE; // adjust the position displayed in the floating window to set wmParams to the top of the left. gravity = Gravity. LEFT | Gravity. TOP; wmParams. x = 0; wmParams. y = 0; // set wmParams for the long and wide data of the floating window. width = WindowManager. layoutParams. MATCH_PARENT; wmParams. height = WindowManager. layoutParams. MATCH_PARENT; v = new MyView (getApplicationContext (); mWindowManager. addView (v, wmParams );}
Flashlight main code
/*** Turn on or off the flashlight */private View. onClickListener flash = new View. onClickListener () {@ Override public void onClick (View v) {// first determine whether a flashlight exists if (! GetPackageManager (). hasSystemFeature (PackageManager. FEATURE_CAMERA_FLASH) {Toast. makeText (MainActivity. this, the current device does not flash off the 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 (new SurfaceTexture (0);} catch (IOException e) {close ();} if (camera! = Null) {parameters = camera. getParameters (); previousflashmode = parameters. getFlashMode () ;}if (previusflashmode = null) {previusflashmode = Camera. parameters. FLASH_MODE_OFF ;}}// close the camera private void close () {if (camera! = Null) {parameters. setFlashMode (previusflashmode); camera. setParameters (parameters); camera. release (); camera = null; flashstate = false ;}// open the 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 ;}}