我如何使闪光灯闪光灯打开/关闭?我尝试使用for循环但没有工作......冻结...... 为什么?需要报警管理器?线程可运行的帖子延迟了吗?
for(int i=0;i<5;i++){
flashon();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
flashoff();
}
}
public void flashon(){
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
}
答案 0 :(得分:2)
您需要使用Handler
Handler handler;
boolean isFlashon=false;
handler = new Handler();
handler.removeCallbacks(toggleFlash);
handler.postDelayed(toggleFlash, 1000 * 60);
和
private Runnable toggleFlash = new Runnable() {
public void run() {
if(isFlashon)
{
//do Flash off
isFlashon=false;
}
else
{
//do Flash on
isFlashon=true;
}
handler.postDelayed(this, 1000 * 60);
}
};
答案 1 :(得分:1)
尝试下面的代码。它会工作,因为我也面临类似的情况,以下代码为我工作。
private boolean isFlashOn;
private Camera camera;
Parameters params;
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
}
}
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
}
}
public void flash_effect() throws InterruptedException
{
Thread a = new Thread()
{
public void run()
{
for(int i =0; i < 50; i++)
{
turnOnFlash();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
turnOffFlash();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
a.start();
}
答案 2 :(得分:0)
1.开启
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
2。关闭
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
并且,在AndroidManifest.xml上添加以下权限。
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
请参阅此http://www.mkyong.com/android/how-to-turn-onoff-camera-ledflashlight-in-android/