Android 打开关闭闪光灯(里程碑2.1)
2012-06-27 14:14
471 查看
来源:http://wjhhanbao.iteye.com/blog/1195666
不同的手机,开启闪光灯的方法不一样,这里以摩托罗拉里程碑的手机为例
main.xml:
Activity代码:
封装的方法:
其他一些手机开启闪光灯的方法可以参考 /article/7947799.html
不同的手机,开启闪光灯的方法不一样,这里以摩托罗拉里程碑的手机为例
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/open" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/open" /> <Button android:id="@+id/close" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/close" /> </LinearLayout>
Activity代码:
package com.android.flashlight; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class AndroidFlashLightActivity extends Activity { /** Called when the activity is first created. */ private Button mBtnOpen,mBtnClose; private MyFlashLight myFlashLight; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mBtnOpen = (Button) findViewById(R.id.open); mBtnClose = (Button) findViewById(R.id.close); try { myFlashLight = new MyFlashLight(); } catch (Exception e) { e.printStackTrace(); } mBtnOpen.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { if(myFlashLight.isEnabled() == false){ myFlashLight.enable(true); } } }); mBtnClose.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { if(myFlashLight.isEnabled() == true){ myFlashLight.enable(false); } } }); } }
封装的方法:
package com.android.flashlight; import java.lang.reflect.Method; import android.os.IBinder; public class MyFlashLight { private Object svc = null; private Method getFlashlightEnabled = null; private Method setFlashlightEnabled = null; @SuppressWarnings("unchecked") public MyFlashLight() 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 proxy Class 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 methods Class proxy = svc.getClass(); // save methods getFlashlightEnabled = 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) {} } }
其他一些手机开启闪光灯的方法可以参考 /article/7947799.html
相关文章推荐
- Android 打开关闭闪光灯(里程碑2.1)
- Android 打开关闭闪光灯。
- Android 打开和关闭闪光灯
- Android系统移植与调试之------->增加一个双击物理按键打开和关闭闪光灯并将闪光灯状态同步到下拉菜单中
- android 判断是否有闪光灯,打开或者关闭闪光灯
- Android控制闪光灯的方法(打开与关闭)
- android学习:Activity简单操作---打开、关闭一个新的Activity
- Android Virtual Device Manager失去响应后,被强制关闭,之后无法打开Android Virtual Device Manager
- Android之打开闪光灯关键代码
- Android KeyBoardUtils-打开或关闭软键盘
- Android中如何通过程序打开或关闭GPS
- android中如何用代码来关闭打开的相机
- Android中如何通过程序打开或关闭GPS
- [Android] 软键盘打开与关闭的方法
- android打开关闭蓝牙和wifi
- Android -- Wifi热点的打开与关闭流程简介
- Android属性设置android:noHistory="true",打开下一个Activity关闭本Activity
- 9.1、Android中得到新打开Activity 关闭后返回的数据
- 如何在Android中获知屏幕打开或者关闭
- Android程序:使用系统服务*1.获取网络状态 * 2.打开关闭wifi * 3.获取系统音量 * 4.获取运行程序的包名