這是您遇到的問題的link。 「關鍵是將狀態改回FLASH_MODE_OFF
,然後回到FLASH_MODE_TORCH
。」
在解決方案中,他創建了一個Timer Task
來處理檢查屏幕是否打開。然後,他關掉閃光燈,然後再打開。
低於鏈接的解決方案是另一個solution,它添加了一個thread
並使其在發送torch
命令之前進入睡眠200毫秒。
所以我想說你正在尋找的解決方案是兩種解決方案的組合。
@Override
public void onCreate() {
// assume we start with screen on and save that state ;-)
this.pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
screenOn = this.pm.isScreenOn();
// program a timer which checks if the light needs to be re-activated
this.mTimer = new Timer();
this.mTimerTask = new TimerTask() {
public void run() {
// re-activate the LED if screen turned off
if(!pm.isScreenOn() && pm.isScreenOn() != screenOn) {
Log.i("SleepLEDservice", "re-activated the LED");
// really it's NOT ENOUGH to just "turn it on", i double-checked this
setFlashlight(Camera.Parameters.FLASH_MODE_OFF);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
setFlashlight(Camera.Parameters.FLASH_MODE_TORCH);
}
screenOn = pm.isScreenOn();
}
};
}
private void setFlashlight(String newMode) {
try {
this.frontCamPara = this.frontCam.getParameters();
if(this.frontCamPara.getFlashMode() != newMode) {
this.frontCamPara.setFlashMode(newMode);
this.frontCam.setParameters(frontCamPara);
}
} catch (Exception e) {
e.printStackTrace();
}
}
再次,信貸應該去@stefanjunker和@BlueJam作爲他們的答案在這引用。
太棒了..謝謝大家:) –