How to enable flashlight for Android

Source: Internet
Author: User

1. Use ihardwareservice for direct control

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:

[Java]
View plaincopy
  1. <Span style = "font-size: 18px;"> package Android. OS;
  2. /** {@ Hide }*/
  3. Interface ihardwareservice
  4. {
  5. // Obsolete flashlight support
  6. Boolean getflashlightenabled ();
  7. Void setflashlightenabled (Boolean on );
  8. } </Span>

Then introduce

[Java]
View plaincopy
  1. Import Android. OS. ihardwareservice;
  2. /**
  3. * Enable and disable the flashlight.
  4. * @ Param isenable
  5. * @ Author Linc
  6. * @ Date 2012-3-18
  7. */
  8. Private void setflashlightenabled (Boolean isenable)
  9. {
  10. Try
  11. {
  12. Method method = Class. forname ("android. OS. servicemanager"). getmethod ("getservice", String. Class );
  13. Ibinder binder = (ibinder) method. Invoke (null, new object [] {"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:

[HTML]
View plaincopy
  1. <Span style = "font-size: 18px;"> <uses-Permission Android: Name = "android. Permission. Flashlight"/>
  2. <Uses-Permission Android: Name = "android. Permission. hardware_test"/> </span>

2. Use a camera to control the flashlight

This is another direction of thinking. Camera comes with a variety of interfaces and parameters. We just need to use them.

[Java]
View plaincopy
  1. <Span style = "font-size: 18px;"> private camera = NULL;
  2. Private parameters = NULL;
  3. // Directly enable
  4. Camera = camera. open ();
  5. Parameters = camera. getparameters ();
  6. Parameters. setflashmode (parameters. flash_mode_torch); // enable
  7. Camera. setparameters (parameters );
  8. // Close directly
  9. Parameters. setflashmode (parameters. flash_mode_off); // close
  10. Camera. setparameters (parameters );
  11. Camera. Release (); </span>

If you encounter this problem, you can try the preview method of camera:

[Java]
View plaincopy

  1. Private void openlighton (){
  2. If (null = m_camera)
  3. {
  4. M_camera = camera. open ();
  5. }
  6. Camera. parameters = m_camera.getparameters ();
  7. Parameters. setflashmode (camera. Parameters. flash_mode_torch );
  8. M_camera.setparameters (parameters );
  9. M_camera.autofocus (new camera. autofocuscallback (){
  10. Public void onautofocus (Boolean success, camera ){
  11. }
  12. });
  13. M_camera.startpreview ();
  14. }
  15. Private void closelightoff (){
  16. If (m_camera! = NULL)
  17. {
  18. M_camera.stoppreview ();
  19. M_camera.release ();
  20. M_camera = NULL;
  21. }
  22. }

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.