Enable flashlight for Android source code

Source: Internet
Author: User

Permission:

<Uses-permissionandroid: Name = "android. Permission. Flashlight"/>

<Uses-permissionandroid: Name = "android. Permission. wake_lock"/>

<Uses-Permission Android: Name = "android. Permission. Camera"/>

<Uses-featureandroid: Name = "android. Hardware. Camera"/>

 

Layout

<? XML version = "1.0" encoding = "UTF-8"?>

<Linearlayoutxmlns: Android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: gravity = "center"

Android: Orientation = "vertical">

 

<Togglebutton

Android: Id = "@ + ID/togglebutton1"

Android: layout_width = "match_parent"

Android: layout_height = "match_parent"

Android: checked = "true"

Android: text = "togglebutton"/>

 

</Linearlayout>

 

Source code

Package CN. Lee. handlight;

 

Import Android. App. activity;

Import Android. Hardware. camera;

Import Android. Hardware. Camera. parameters;

Import Android. OS. Bundle;

Import Android. OS. process;

Import Android. View. view;

Import Android. View. View. onclicklistener;

Import Android. View. windowmanager;

Import Android. widget. togglebutton;

 

Public class handlightactivity extends activityimplements onclicklistener {

Privatetogglebutton togglebutton;

 

Privatecamera camera = camera. open ();

/** Called when the activity is first created .*/

@ Override

Publicvoid oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

 

Togglebutton = (togglebutton) This. findviewbyid (R. Id. togglebutton1 );

Togglebutton. setonclicklistener (this );

Getwindow (). addflags (windowmanager. layoutparams. flag_keep_screen_on );

// Keep the screen

}

 

@ Override

Publicvoid onclick (view v ){

Togglebuttontb = (togglebutton) V;

Camera. parametersparam = camera. getparameters ();

If (! TB. ischecked ()){

Param. setflashmode (camera. Parameters. flash_mode_torch );

Togglebutton. setbackgroundcolor (0x30ffffff );

} Else {

Param. setflashmode (camera. Parameters. flash_mode_off );

Togglebutton. setbackgroundcolor (0 xffffffff );

}

Camera. setparameters (PARAM );

}

 

@ Override

Protectedvoid onpause (){

// Camera. Release ();

// Process. killprocess (process. mypid ());

Super. onpause ();

}

}

 

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.