Android Flashlight Source

Source: Internet
Author: User

Recently, because the company needs to do a flashlight, in fact, the flashlight principle is very simple, is to call the camera flash, control the flash switch, you can achieve the effect of flashlight,

Emphasize that it is important to note that the Flash is released at the end of the code, otherwise it will occur when the camera is used--the problem of not connecting to the camera

Flashlight APK: Click to open the download link

Flashlight Project Source code download: 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.Looper; 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 bgma Geview = 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 (Looper.getmainlooper ()) {@Overridepublic void Handlemessage (Message msg) {switch (msg.what {Case Open_camera:camera = Camera.open (); break;case open_light:parameters params = camera.getparameters (); list<string> 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 Flash mode", Toast.length_short). Show (); Camera.setparameters (params); Camera.startpreview (); isOpen = True;break;case close_light:if (isOpen) {Parameters Closepparameters = Camera.getparameters (); Closepparameters.setflashmode (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-generated 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 permissions you need to use are as follows:

<uses-permission android:name= "Android.permission.CAMERA"/>    <uses-permission android:name= " Android.permission.FLASHLIGHT "/>    <uses-feature android:name=" Android.hardware.camera "/>    < Uses-feature android:name= "Android.hardware.camera.autofocus"/>    <uses-feature android:name= " Android.hardware.camera.flash "/>       <uses-permission android:name=" Android.permission.VIBRATE "/>     

The interface layout is as follows:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:background= "@drawable/bg_flashlight" >    <relativelayout        android:id= "@+id/bg_image"        android:layout_width= "match_parent"        android:layout_height= "match_parent"        android:background= "@ Drawable/bg_flashlight_off ">        <imageview            android:id=" @+id/btn_image "            android:layout_width=" Wrap_content "            android:layout_height=" wrap_content "           android:layout_centerhorizontal=" true "             Android:layout_alignparentbottom= "true"             android:layout_marginbottom= "100dip"            android:src= "@drawable/ btn_flash_light_on "/>    </RelativeLayout></RelativeLayout>

The effect of the implementation is as follows:



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.