Android --- eye lamp and high compatibility flashlight

Source: Internet
Author: User

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 ;}}

 

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.