android闪光灯参数,Android-关于android调用闪光灯的问题

博客给出了一段Android控制闪光灯的代码,通过反射调用相关方法实现闪光灯的开启和关闭。代码来自特定网址,且指出不同类别手机开启闪光灯方法可能不同,此代码适用于mb525手机。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.wjh.myset;

import java.io.PrintStream;

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://code.google.com/p/search-light/

不同类别的手机开启闪光灯的方法不一定相同,下面的代码适用于mb525,其他的手机我还没有试过.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值