MB526上的手电筒软件

来源:互联网 发布:linux没有 telnet命令 编辑:程序博客网 时间:2024/04/29 07:57

本文参考至:http://download.csdn.net/detail/chenxupro/3884639,一个下载链接

网上看过N多个文章,但均不适用于MB526,上面的资源刚好符合,系统是2.3.6的,不保障适用于所有系统,其他手机很多都是利用相机来实现手电筒功能的,但MB526就是不可以,只能用这种方式了。

class DroidLED {private Object svc = null;private Method getFlashlightEnabled = null;private Method setFlashlightEnabled = null;@SuppressWarnings("unchecked")public DroidLED() throws Exception {try {// call ServiceManager.getService("hardware") to get an IBinder for the service.// this appears to be totally undocumented and not exposed in the SDK whatsoever.Class sm = Class.forName("android.os.ServiceManager");Object hwBinder = sm.getMethod("getService", String.class).invoke(null, "hardware");// get the hardware service stub. this seems to just get us one step closer to the proxyClass hwsstub = Class.forName("android.os.IHardwareService$Stub");Method asInterface = hwsstub.getMethod("asInterface", android.os.IBinder.class);svc = asInterface.invoke(null, (IBinder) hwBinder);// grab the class (android.os.IHardwareService$Stub$Proxy) so we can reflect on its methodsClass proxy = svc.getClass();// save methodsgetFlashlightEnabled = proxy.getMethod("getFlashlightEnabled");setFlashlightEnabled = proxy.getMethod("setFlashlightEnabled", boolean.class);}catch(Exception e) {throw new Exception("LED could not be initialized");}}public boolean isEnabled() {try {return getFlashlightEnabled.invoke(svc).equals(true);}catch(Exception e) {return false;}}public void enable(boolean tf) {try {setFlashlightEnabled.invoke(svc, tf);}catch(Exception e) {}}}
通过该类实现对闪光灯的控制,在activity中定义DroidLED对象,利用enable(boolen tf)接口来实现对闪光灯的控制,传递true,打开闪关灯;传递false则关闭闪光灯。此外,加上闪光灯权限即可

<uses-permission android:name="android.permission.FLASHLIGHT" />

ps:http://download.csdn.net/detail/key123zhangxing/4625001,糅合别人的代码和图片资源,欢迎下载。