Enable flashlight for Android

Source: Internet
Author: User

Public class handlightactivity extends activity implements onclicklistener {

Private 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 = 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

<? 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 = "vertical">
<Togglebutton Android: Id = "@ + ID/togglebutton1"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: checked = "true"
Android: text = "togglebutton"/>
</Linearlayout>

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:

  1. PackageAndroid. OS;
  2. /** {@ Hide }*/
  3. InterfaceIhardwareservice
  4. {
  5. // Obsolete flashlight support
  6. BooleanGetflashlightenabled ();
  7. VoidSetflashlightenabled (BooleanOn );
  8. }

Then introduce

  1. ImportAndroid. OS. ihardwareservice;
  2. /**
  3. * Enable and disable the flashlight.
  4. * @ Param isenable
  5. * @ Author Linc
  6. * @ Date 2012-3-18
  7. */
  8. Private VoidSetflashlightenabled (BooleanIsenable)
  9. {
  10. Try
  11. {
  12. Method method = Class. forname ("android. OS. servicemanager"). getmethod ("getservice", String.Class);
  13. Ibinder binder = (ibinder) method. Invoke (Null,NewObject [] {"hardware "});
  14. Ihardwareservice localhardwareservice = ihardwareservice. stub. asinterface (binder );
  15. Localhardwareservice. setflashlightenabled (isenable );
  16. }
  17. Catch(Exception E)
  18. {
  19. E. printstacktrace ();
  20. }
  21. }

In this way, you can turn on the flashlight. Permissions to be used:

  1. <Uses-PermissionAndroid: Name = "android. Permission. Flashlight"/>
  2. <Uses-PermissionAndroid: Name = "android. Permission. hardware_test"/>

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.