How to enable flashlight for Android

Source: Internet
Author: User

Copy codeThe Code is as follows: public class HandLightActivity extends Activity implements OnClickListener {
Private ToggleButton toggleButton;
Private Camera m_Camera;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. handlight );
ToggleButton = (ToggleButton) this. findViewById (R. id. toggleButton1 );
ToggleButton. setOnClickListener (this );
GetWindow (). addFlags (WindowManager. LayoutParams. FLAG_KEEP_SCREEN_ON );
@ Override
Public void onClick (View v ){
ToggleButton tb = (ToggleButton) v;
If (! Tb. isChecked ()){
PackageManager pm = this. getPackageManager ();
FeatureInfo [] features = pm. getSystemAvailableFeatures ();
For (FeatureInfo f: features)
If (PackageManager. FEATURE_CAMERA_FLASH.equals (f. name) // determine whether the device supports flashlight
If (null = m_Camera)
M_Camera = Camera. open ();

Camera. Parameters parameters = m_Camera.getParameters ();
Parameters. setFlashMode (Camera. Parameters. FLASH_MODE_TORCH );
M_Camera.setParameters (parameters );
M_Camera.startPreview ();
ToggleButton. setBackgroundColor (0x30ffffff );
} Else {
If (m_Camera! = Null)
M_Camera.stopPreview ();
M_Camera.release ();
M_Camera = null;
ToggleButton. setBackgroundColor (0 xffffffff );

Handlight. xml
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = ""
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<ToggleButton android: id = "@ + id/toggleButton1"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: checked = "true"
Android: text = "ToggleButton"/>

Add Permissions
<Uses-permission android: name = "android. permission. FLASHLIGHT"/>
<Uses-permission android: name = "android. permission. WAKE_LOCK"/>
<Uses-permission android: name = "android. permission. CAMERA"/>
<Uses-permission android: name = "android. hardware. camera"/>
Note: not all devices support

There is also a saying on the Internet that the system hidden api is called, but I did not respond, it may be a problem with the system version, back up
Direct Control Using IHardwareService
Android1.5 previously provided this interface directly. Later, we need to do it ourselves.
Create a new Android. OS package in your project and create an IHardwareService. aidl file. The content is as follows:Copy codeThe Code is as follows: package android. OS;

/** {@ Hide }*/
Interface IHardwareService
// Obsolete flashlight support
Boolean getFlashlightEnabled ();
Void setFlashlightEnabled (boolean on );

Then introduce
Copy codeThe Code is as follows: import android. OS. IHardwareService;

* Enable and disable the flashlight.
* @ Param isEnable
* @ Author linc
* @ Date 2012-3-18
Private void setFlashlightEnabled (boolean isEnable)
Method method = Class. forName ("android. OS. ServiceManager"). getMethod ("getService", String. class );
IBinder binder = (IBinder) method. invoke (null, new Object [] {"hardware "});

IHardwareService localhardwareservice = IHardwareService. Stub. asInterface (binder );
Localhardwareservice. setFlashlightEnabled (isEnable );
Catch (Exception e)
E. printStackTrace ();


In this way, you can turn on the flashlight. Permissions to be used:
Copy codeThe Code is as follows: <uses-permission android: name = "android. permission. FLASHLIGHT"/>
<Uses-permission android: name = "android. permission. HARDWARE_TEST"/>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.