不同的手机,开启闪光灯的方法不一样,这里以摩托罗拉里程碑的手机为例
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代码:

[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 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. }

封装的方法:

[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 undocumented 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 could 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. }

Android 打开关闭闪光灯(里程碑2.1)相关推荐

  1. Android打开/关闭数据流量

    这个是网上找的,经过自己整理.由于android并未公开这个类,因此如果要打开/关闭数据,需要通过反射 16/09/29注:5.0以上无效 MobileDataSwitcher.java import ...

  2. Android打开/关闭屏幕和设置系统屏幕亮度方法

    在有一次处理屏幕的时候,我通过设置值发现怎么都实现不了. 控制屏幕的两种方法: 第一:通过获取PowerManager对象,利用反射.打开/关闭屏幕,上代码: /**  * 关闭屏幕 ,其实是使系统休 ...

  3. android 打开屏幕,android打开关闭屏幕

    打开和关闭手机屏幕方法: 1.关闭屏幕 //设备管理者 private DevicePolicyManager mDevicePolicyManager; //关屏组件 private Compone ...

  4. Android打开/关闭免提(SpeakerPhone)

    //打开扬声器      public void OpenSpeaker() { try{          AudioManager audioManager = (AudioManager) mC ...

  5. Android 打开关闭硬件加速

    尊重原创,原文出处:http://www.cnblogs.com/frydsh/archive/2012/10/23/2733581.html Android从3.0(API Level 11)开始, ...

  6. Android打开/关闭wifi命令

    1.切换root权限 # adb root; adb remount 2.关闭wifi # adb shell svc wifi disable 3.打开wifi # adb shell svc wi ...

  7. android 打开闪光灯

    在android中打开闪光灯的方法有两种,一种是获取硬件服务,通过反射的方式来操作闪光灯.另外一种是获得Camera对象,通过设置Camera的参数来操作闪光灯.一下是一个操作闪光灯的工具类:实现了两 ...

  8. Android 获取光线强弱,开启闪光灯,关闭闪光灯

    1.获取光线强度 (1)实现传感器接口  implements SensorEventListener 实例化 private SensorManager mSensroMgr;//传感器管理类 mS ...

  9. Android WiFi 打开关闭流程

    本文简单介绍下WiFi打开与关闭流程,参考源码Android P. 一.WiFi 开机自动打开流程 系统服务启动的时候会启动WifiService,在SystemService.PHASE_SYSTE ...

最新文章

  1. Unity 2D游戏开发教程之摄像头追踪功能
  2. (一)安装docker
  3. java 自己抛空指针异常_java Timetask 访问service 抛空指针异常解决方案
  4. UA MATH563 概率论的数学基础I 概率空间1 基本概念
  5. [转]python yield
  6. 假设检验在数据分析中的应用
  7. 5.4shell编程3
  8. SQL SERVER 2008筛选时报错 无法为该请求检索数据
  9. Mobx入门之四:自定义reactions,when, autorun
  10. Linux 环境 简单启动 Java程序
  11. 亲和数(220/284)
  12. 前端开发的模块化和组件化的定义,以及两者的关系?
  13. MySQL table 添加_「primarykey」MySQL中为table添加primary key的两种方法 - seo实验室
  14. Python关键字keyword
  15. 如何解决‘CommandNotFoundError: Your shell has not been properly configured to use ‘conda activate’错误
  16. Android Hanlder的理解
  17. java-接口之运动员教练员综合案例
  18. 机器学习排序算法:RankNet to LambdaRank to LambdaMART
  19. Ewebeditor的问题
  20. tl wdr5660虚拟服务器,TP-Link TL-WDR5600端口映射设置教程

热门文章

  1. 自己的第一款微信公众号产品开发完成,已上线,欢迎大家拍砖
  2. Windows Core Audio 音频开发技术指南
  3. 走进编程:C++的发展历程
  4. 中医的诊断技术是落后的,思想方法是片面的
  5. 【Leetcode】589. N叉树前序遍历
  6. iccv2020论文汇总_【论文相关】历年CVPR、ICCV、ECCV论文合集下载
  7. 2.功耗计算之W,mW 理解
  8. 风口上的AIGC,技术人才动不动就年薪百万?
  9. 机器学习_线性回归_回归系数
  10. 智慧屏如何进行智慧双投?