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

来源:http://wjhhanbao.iteye.com/blog/1195666

不同的手机,开启闪光灯的方法不一样,这里以摩托罗拉里程碑的手机为例

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) {}  
	}  
} 

其他一些手机开启闪光灯的方法可以参考 http://blog.csdn.net/guozh/article/details/7379902
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值