Android Turn on flashlight function (perfect fit android4x,5x,6x)

Source: Internet
Author: User

Recently in research to open the Android flashlight function, summed up the next android4x, 5x, 6x flashlight Open method, written in public methods, share to everyone.

Add camera and Flash permissions
<uses-permission android:name="android.permission.FLASHLIGHT" />    <uses-permission android:name="android.permission.CAMERA" />    <uses-feature android:name="android.hardware.camera" />    <uses-feature android:name="android.hardware.autofocus" />
Layout file
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:layout_width  =" match_parent " android:layout_height  =" match_parent " android:orientation  =;     <button  android:id  = "@+id/btn_open"  android:layout_width  = "wrap_content"  android:layout_height  = "wrap_content"  android:layout_marginbottom  = "10DP"  android:text  =" Turn on the flashlight "/>     <buttonandroid:id="@+id/btn_close"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:text=" Turn off flashlight " />                                 </linearlayout>
Specific implementation
/** * Created by monkeys moved by Castiel * Blog address: Http://blog.csdn.net/mynameishuangshuai * * Public  class mainactivity extends appcompatactivity {    PrivateCameramanager Manager;//Declare Cameramanager object    PrivateCamera M_camera =NULL;//Declare camera object    @TargetApi(Build.version_codes. LOLLIPOP)@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Button Btnopen = (button) Findviewbyid (R.id.btn_open);        Button btnclose = (button) Findviewbyid (r.id.btn_close); Manager = (Cameramanager) getsystemservice (Context.camera_service);Try{string[] camerlist = Manager.getcameraidlist (); for(String str:camerlist) {            }        }Catch(Cameraaccessexception e) {LOG.E ("Error", E.getmessage ()); } btnopen.setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View view) {LightSwitch (false);        }        }); Btnclose.setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View view) {LightSwitch (true);    }        }); }/** * Flashlight Control method * * @param lightstatus * @return * *    Private void LightSwitch(Final BooleanLightstatus) {if(Lightstatus) {//Turn off the flashlight            if(Build.VERSION.SDK_INT >= build.version_codes. M) {Try{Manager.settorchmode ("0",false); }Catch(Exception e)                {E.printstacktrace (); }            }Else{if(M_camera! =NULL) {M_camera.stoppreview ();                    M_camera.release (); M_camera =NULL; }            }        }Else{//Turn on the flashlight            if(Build.VERSION.SDK_INT >= build.version_codes. M) {Try{Manager.settorchmode ("0",true); }Catch(Exception e)                {E.printstacktrace (); }            }Else{FinalPackagemanager pm = Getpackagemanager ();FinalFeatureinfo[] features = Pm.getsystemavailablefeatures (); for(FinalFeatureinfo f:features) {if(PackageManager.FEATURE_CAMERA_FLASH.equals (F.name)) {//Determine if the device supports Flash                        if(NULL= = M_camera) {M_camera = Camera.open (); }FinalCamera.parameters Parameters = M_camera.getparameters ();                        Parameters.setflashmode (Camera.Parameters.FLASH_MODE_TORCH);                        M_camera.setparameters (parameters);                    M_camera.startpreview (); }                }            }        }    }/** * Determine if the Android system version >= M (API23) */    Private Boolean IsM() {if(Build.VERSION.SDK_INT >= build.version_codes. M) {return true; }Else{return false; }    }}

Android Turn on flashlight function (perfect fit android4x,5x,6x)

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.