Turn off the flashlight on Android (milestone 2.1)

Source: Internet
Author: User

Different mobile phones use different methods to enable Flash. Here, the Motorola milestone mobile phone is used as an example main. xml:

[HTML]
View plaincopy
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent"
  5. Android: Orientation = "vertical">
  6. <Button
  7. Android: Id = "@ + ID/Open"
  8. Android: layout_width = "fill_parent"
  9. Android: layout_height = "wrap_content"
  10. Android: text = "@ string/Open"/>
  11. <Button
  12. Android: Id = "@ + ID/Close"
  13. Android: layout_width = "fill_parent"
  14. Android: layout_height = "wrap_content"
  15. Android: text = "@ string/Close"/>
  16. </Linearlayout>

Activity Code:

[Java]
View plaincopy
  1. Package com. Android. Flashlight;
  2. Import Android. App. activity;
  3. Import Android. OS. Bundle;
  4. Import Android. View. view;
  5. Import Android. widget. Button;
  6. Public class androidflashlightactivity extends activity {
  7. /** Called when the activity is first created .*/
  8. Private button mbtnopen, mbtnclose;
  9. Private myflashlight;
  10. @ Override
  11. Public void oncreate (bundle savedinstancestate ){
  12. Super. oncreate (savedinstancestate );
  13. Setcontentview (R. layout. Main );
  14. Mbtnopen = (button) findviewbyid (R. Id. Open );
  15. Mbtnclose = (button) findviewbyid (R. Id. Close );
  16. Try {
  17. Myflashlight = new myflashlight ();
  18. } Catch (exception e ){
  19. E. printstacktrace ();
  20. }
  21. Mbtnopen. setonclicklistener (New button. onclicklistener (){
  22. @ Override
  23. Public void onclick (view v ){
  24. If (myflashlight. isenabled () = false ){
  25. Myflashlight. Enable (true );
  26. }
  27. }
  28. });
  29. Mbtnclose. setonclicklistener (New button. onclicklistener (){
  30. @ Override
  31. Public void onclick (view v ){
  32. If (myflashlight. isenabled () = true ){
  33. Myflashlight. Enable (false );
  34. }
  35. }
  36. });
  37. }
  38. }

Encapsulation Method:

[Java]
View plaincopy
  1. Package com. Android. Flashlight;
  2. Import java. Lang. Reflect. method;
  3. Import Android. OS. ibinder;
  4. Public class myflashlight {
  5. Private object SVC = NULL;
  6. Private method getflashlightenabled = NULL;
  7. Private method setflashlightenabled = NULL;
  8. @ Suppresswarnings ("unchecked ")
  9. Public myflashlight () throws exception {
  10. Try {
  11. // Call servicemanager. getservice ("hardware") to get an ibinder for the service.
  12. // This appears to be totally uninitialized ented and not exposed in the SDK whatsoever.
  13. Class Sm = Class. forname ("android. OS. servicemanager ");
  14. Object hwbinder = Sm. getmethod ("getservice", String. Class). Invoke (null, "hardware ");
  15. // Get the hardware service stub. This seems to just get us one step closer to the proxy
  16. Class hwsstub = Class. forname ("android. OS. ihardwareservice $ stub ");
  17. Method asinterface = hwsstub. getmethod ("asinterface", Android. OS. ibinder. Class );
  18. SVC = asinterface. Invoke (null, (ibinder) hwbinder );
  19. // Grab the class (Android. OS. ihardwareservice $ stub $ proxy) so we can reflect on its methods
  20. Class proxy = SVC. getclass ();
  21. // Save Methods
  22. Getflashlightenabled = proxy. getmethod ("getflashlightenabled ");
  23. Setflashlightenabled = proxy. getmethod ("setflashlightenabled", Boolean. Class );
  24. }
  25. Catch (exception e ){
  26. Throw new exception ("led cocould not be initialized ");
  27. }
  28. }
  29. Public Boolean isenabled (){
  30. Try {
  31. Return getflashlightenabled. Invoke (SVC). Equals (true );
  32. }
  33. Catch (exception e ){
  34. Return false;
  35. }
  36. }
  37. Public void enable (Boolean TF ){
  38. Try {
  39. Setflashlightenabled. Invoke (SVC, TF );
  40. }
  41. Catch (exception e ){}
  42. }
  43. }

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.