闪光灯降功耗方案
来源:互联网 发布:汽车分期2016年数据 编辑:程序博客网 时间:2024/06/03 19:39
在android中打开闪光灯的方法有两种,一种是获取硬件服务,通过反射的方式来操作闪光灯。另外一种是获得Camera对象,通过设置Camera的参数来操作闪光灯。一下是一个操作闪光灯的工具类:实现了两种方式操作闪光灯。通过switchFlashlight方法是通过反射的方式操作,通过turnLightOn,turnLightOff方法操作是通过设置Camera来操作闪关灯的。 为了达到将功耗的目的,需要把闪光灯打开的方式从操作Camera改为直接获得硬件结点进行操作的方式。成立由驱动工程师和应用工程师组成的小组,设计出通过在应用中添加额外放射代码,以及在系统服务CameraService中添加直接操作硬件结点/dev/flashlight的方式 控制FlashLight开关的方案,最终顺利完成功耗电流测试。 private static final Object iHardwareService; private static final Method setFlashEnabledMethod; static { iHardwareService = getHardwareService(); //静态代码块 获得Method方法 获得ICameraService的接口 setFlashEnabledMethod = getSetFlashEnabledMethod(iHardwareService); if (iHardwareService == null) { Log.v(TAG, "This device does supports control of a flashlight"); } else { Log.v(TAG, "This device does not support control of a flashlight"); } } /** * 通过反射来操作闪光灯 */ private static Object getHardwareService() { // 反射获得Class对象 Class<?> serviceManagerClass = maybeForName("android.os.ServiceManager"); if (serviceManagerClass == null) { return null; } //从Class对象获得Method对象 Method getServiceMethod = maybeGetMethod(serviceManagerClass, "getService", String.class); if (getServiceMethod == null) { return null; } // 从Method 对象获得cameraservice服务的Object对象 Object hardwareService = invoke(getServiceMethod, null, "cameraservice"); if (hardwareService == null) { return null; } // 获得ICameraService的Stub的Class Class<?> iHardwareServiceStubClass = maybeForName("android.os.ICameraService$Stub"); if (iHardwareServiceStubClass == null) { return null; } //获得 ICameraService的asInterface 最终回返回一个客户端的操作类 Method asInterfaceMethod = maybeGetMethod(iHardwareServiceStubClass, "asInterface", IBinder.class); if (asInterfaceMethod == null) { return null; } return invoke(asInterfaceMethod, null, hardwareService); } public static void switchFlashlight(boolean active) { setFlashlight(active); } static void disableFlashlight() { setFlashlight(false); } private static void setFlashlight(boolean active) { if (iHardwareService != null) { invoke(setFlashEnabledMethod, iHardwareService, active); } } } private static Object invoke(Method method, Object instance, Object... args) { try { return method.invoke(instance, args); } catch (Exception e) { Log.w(TAG, "Unexpected error while invoking " + method, e); return null; } } private static Method getSetFlashEnabledMethod(Object iHardwareService) { if (iHardwareService == null) { return null; } Class<?> proxyClass = iHardwareService.getClass(); return maybeGetMethod(proxyClass, "setFlashlightEnabled", boolean.class); } private static Class<?> maybeForName(String name) { try { return Class.forName(name); } catch (ClassNotFoundException cnfe) { // OK return null; } catch (Exception re) { re.printStackTrace(); Log.w(TAG, "Unexpected error while finding class " + name, re); return null; } }
阅读全文
0 0
- 闪光灯降功耗方案
- 闪光灯
- 闪光灯
- Android 开启闪光灯比较完美的兼容方案
- NXP LPC1765 + rtx内核 低功耗方案
- 君正超低功耗电池wifi可视门铃方案
- 低功耗电池类移动视频方案
- 功耗
- 低功耗wifi可视门铃方案全球推广
- RK3066和AML8726-MX方案对比 频率与功耗 / 性能 / 方案成本
- 什么是闪光灯
- 闪光灯控制
- wp7闪光灯
- iphone 闪光灯
- 调用闪光灯
- iOS 闪光灯
- ios闪光灯
- 开FPU后功耗降不下来
- 我的Java数据结构和算法
- 【LeeCode】Reverse Integer 总结
- Git学习(一)
- Docker: 限制容器可用的 CPU
- eclipse配置Struts2至Tomcat8.5 全过程搭建
- 闪光灯降功耗方案
- LA 3938 "Ray, Pass me the dishes!"
- shell日期处理:给定日期推周几日期、求上周周几日期
- 跨域资源共享CORS学习笔记
- 向量积
- Swing JTable 简单使用
- Maven Update Project...后出现Java Resources有红叉提示
- 大整数的四则运算(C语言实现)(1)——大整数的输入处理
- openvc操作图片像素----之图像灰度处理