Android practice simple tutorial-57th guns (share Xiaomi flashlight source code), android flashlight
The implementation of the flashlight function is simple and practical. Next we will study how to make a flashlight program. Beginners can learn about the principle through examples to improve their enthusiasm and motivation for learning android technology.
First, let's take a look at the android permissions that the flashlight needs to call:
<! -- Open the permission of Camera --> <uses-permission android: name = "android. permission. CAMERA "/> <uses-feature android: name =" android. hardware. camera "/> <uses-feature android: name =" android. hardware. autofocus "/> <! -- Enable the FLASHLIGHT permission --> <uses-permission android: name = "android. permission. FLASHLIGHT"/> <uses-permission android: name = "android. permission. DISABLE_KEYGUARD"/>
The above three are camera permissions, and the following two are flashlight permissions, which must be configured in this instance.
Let's take a look at MainActivity. java:
Package com. zxx1.light; import android. app. activity; import android. hardware. camera; import android. hardware. camera. parameters; import android. OS. bundle; import android. view. keyEvent; import android. view. view; import android. view. view. onClickListener; import android. view. window; import android. view. windowManager; import android. widget. button; import android. widget. toast; public class LightActivity extends Acti Optional {private Button lightBtn = null; private Camera camera = null; private Parameters parameters = null; public static boolean isLight = true; // define the switch status. The status is false, the status is true, and the off status is private int back = 0; // @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // full screen setting, hiding all the decoration of the window getWindow (). setFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, WindowManager. layoutParam S. FLAG_FULLSCREEN); requestWindowFeature (Window. FEATURE_NO_TITLE); // set the screen display without a title. You must set the title when it is enabled. Otherwise, getWindow () cannot be set again (). setFlags (WindowManager. layoutParams. FLAG_DISMISS_KEYGUARD, WindowManager. layoutParams. FLAG_DISMISS_KEYGUARD); getWindow (). setFlags (WindowManager. layoutParams. FLAG_KEEP_SCREEN_ON, WindowManager. layoutParams. FLAG_KEEP_SCREEN_ON); // keep the screen on. setContentView (R. layout. main); lightBtn = (Button) findView ById (R. id. btn_light); lightBtn. setOnClickListener (new OnClickListenerImpl ();} class OnClickListenerImpl implements OnClickListener {@ Overridepublic void onClick (View v) {if (isLight) {lightBtn. setBackgroundResource (R. drawable. shou_on); // set camera = Camera for different images. open (); // turn on the flashlight parameters = camera. getParameters (); parameters. setFlashMode (Parameters. FLASH_MODE_TORCH); // enable camera. setParameters (parameters); Camera. startPreview (); // enable isLight = false;} else {lightBtn. setBackgroundResource (R. drawable. shou_off); parameters. setFlashMode (Parameters. FLASH_MODE_OFF); // disable camera. setParameters (parameters); camera. stopPreview (); isLight = true; camera. release () ;}}@ Overrideprotected void onDestroy () {camera. release (); super. onDestroy () ;}@ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK) {back ++; switch (back) {case 1: Toast. makeText (LightActivity. this, getString (R. string. again_exit), Toast. LENGTH_SHORT ). show (); break; case 2: back = 0; DulBack (); break;} return true;} else {return super. onKeyDown (keyCode, event) ;}} public void DulBack () {// close the program if (isLight) {// LightActivity when the switch is off. this. finish (); android. OS. process. killProcess (android. OS. process. myPid (); // closes the process} els E if (! IsLight) {// camera when the switch is on. release (); LightActivity. this. finish (); android. OS. process. killProcess (android. OS. process. myPid (); // shut down the process isLight = true; // avoid this problem. After the switch is enabled, the program exits. If the switch is not enabled again, the program exits }}}
The layout file is as follows:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativelayout" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/btn_light" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:background="@drawable/shou_off" /></RelativeLayout>
Finally, do not forget to Configure permissions. Finally, run the instance as follows:
Follow me to your favorite friends! Thank you.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.