Android Combat Simple Tutorial-the 57th gun (share millet flashlight Source)

Source: Internet
Author: User

Flashlight function is simple and practical, the following we will study how to make a flashlight small program, beginners can learn the principle through an example, improve the enthusiasm and motivation to learn Android technology.

First, let's look at what Android permissions the flashlight will invoke:

  <!--open CAMERA permissions--    <uses-permission android:name= "Android.permission.CAMERA"/>    < Uses-feature android:name= "Android.hardware.camera"/>    <uses-feature android:name= " Android.hardware.autofocus "/>    <!--turn on flash rights--    <uses-permission android:name=" Android.permission.FLASHLIGHT "/>    <uses-permission android:name=" Android.permission.DISABLE_KEYGUARD "/ >    
The above three is the camera permissions, the following two is the Flash permission, in this example to be configured.

Here's 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 Activity {private Button lightbtn = null;private Camera camera = null;private Parameters Parameters = null;public static Boolean islight = true; Define switch state, status false, open state, status true, off state private int back = 0;//@Overridepublic void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate);//Fullscreen settings, hidden window all decorations GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_ Fullscreen, WindowManager.LayoutParams.FLAG_FULLSCREEN); Requestwindowfeature (Window.feature_no_title); Settings screen display Untitled, must be started to set up, otherwise it can not be set GetWindow (). 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 solid Setcontentview (r.layout.main); lightbtn = (Button) Findviewbyid (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 different picture Camera = Camera.open ();//turn on flash parameters = Camera.getparameters ();p Arameters.setflashmode (parameters.flash_mode_torch);//Open Camera.setparameters ( parameters); Camera.startpreview ();//Turn on islight = false;} else {lightbtn.setbackgroundresource (r.drawable.shou_off);p Arameters.setflashmode (Parameters.flash_mode_off);// Close 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 Program if (islight) {//Switch off when LightActivity.this.finish (); Android.os.Process.killProcess ( Android.os.Process.myPid ());//Close process} else if (!islight) {//switch on camera.release (); LightActivity.this.finish (); Android.os.Process.killProcess (Android.os.Process.myPid ());//close Process Islight = true;//Avoid , open the switch after exiting the program, again into the do not open the switch to exit directly when the program error}}}

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, the last running instance is as follows:


Favorite friends pay attention to me! Thank you.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Combat Simple tutorial-57th gun (share Millet flashlight source)

Related Article

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.