Recently, due to the need of the company, we made a flashlight. In fact, the principle of the flashlight is very simple. We can call the camera flashlight to control the flashlight switch to achieve the flashlight effect,
Note that the flash must be released at the end of the Code; otherwise, the camera may appear --Unable to connect to cameraThis problem
Flashlight APK: Click to open the download link
Download the flashlight project source code: Click to open the download link
The main code is as follows:
Package com. techainsh. flashlight; import java. util. list; import android. hardware. camera; import android. hardware. camera. parameters; import android. OS. bundle; import android. OS. handler; import android. OS. logoff; import android. OS. message; import android. OS. powerManager; import android. OS. vibrator; import android. app. activity; import android. content. context; import android. view. view; import android. view. view. onClickListener; import android. widget. imageView; import android. widget. relativeLayout; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {public ImageView btnimageView = null; public RelativeLayout bgmageView = null; private Camera camera; private boolean isOpen = true; public final static int OPEN_CAMERA = 1011; public final static int OPEN_LIGHT = 1012; public final static int CLOSE_LIGHT = 1013; private Vibrator vibrator; long [] pattern = {100,200}; PowerManager. wakeLock wakeLock; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); handler. sendEmptyMessage (OPEN_CAMERA); vibrator = (Vibrator) getSystemService (Context. VIBRATOR_SERVICE); btnimageView = (ImageView) findViewById (R. id. btn_image); bgmageView = (RelativeLayout) findViewById (R. id. bg_image); bgmageView. setBackgroundResource (R. drawable. bg_flashlight_off); btnimageView. setImageResource (R. drawable. btn_flash_light_off); btnimageView. setOnClickListener (this); bgmageView. setBackgroundResource (R. drawable. bg_flashlight_on); btnimageView. setImageResource (R. drawable. btn_flash_light_on); handler. sendEmptyMessage (OPEN_LIGHT); vibrator. vibrate (pattern,-1);} Handler handler = new Handler (logoff. getMainLooper () {@ Overridepublic void handleMessage (Message msg) {switch (msg. what) {case OPEN_CAMERA: camera = Camera. open (); break; case OPEN_LIGHT: Parameters params = camera. getParameters (); List
List = params. getSupportedFlashModes (); if (list. contains (Parameters. FLASH_MODE_TORCH) {params. setFlashMode (Parameters. FLASH_MODE_TORCH);} else {Toast. makeText (getApplicationContext (), "this device does not support flashlight mode", Toast. LENGTH_SHORT ). show ();} camera. setParameters (params); camera. startPreview (); isOpen = true; break; case CLOSE_LIGHT: if (isOpen) {Parameters closepParameters = camera. getParameters (); closepParameters. set FlashMode (Camera. parameters. FLASH_MODE_OFF); camera. setParameters (closepParameters); camera. stopPreview (); // camera. release (); isOpen = false; // sendEmptyMessage (OPEN_CAMERA);} break; default: break ;}};@ Overrideprotected void onDestroy () {super. onDestroy (); if (null! = Camera) {camera. release (); camera = null ;}} public void onStop () {super. onStop (); vibrator. cancel (); if (null! = Camera) {camera. release (); camera = null;} MainActivity. this. finish () ;}@ Overrideprotected void onResume () {// TODO Auto-generated method stubsuper. onResume (); PowerManager pm = (PowerManager) getSystemService (POWER_SERVICE); wakeLock = pm. newWakeLock (PowerManager. SCREEN_BRIGHT_WAKE_LOCK | PowerManager. ON_AFTER_RELEASE, "test"); wakeLock. acquire () ;}@ Overrideprotected void onPause () {// TODO Auto-generat Ed method stubsuper. onPause (); if (wakeLock! = Null) wakeLock. release () ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. btn_image: if (isOpen) {bgmageView. setBackgroundResource (R. drawable. bg_flashlight_off); btnimageView. setImageResource (R. drawable. btn_flash_light_off); handler. sendEmptyMessage (CLOSE_LIGHT); vibrator. vibrate (pattern,-1);} else {bgmageView. setBackgroundResource (R. drawable. bg_flashlight_on); btnimageView. setImageResource (R. drawable. btn_flash_light_on); handler. sendEmptyMessage (OPEN_LIGHT); vibrator. vibrate (pattern,-1) ;}break; default: break ;}}}
The required permissions are as follows:
The interface layout is as follows:
The implementation result is as follows: