Android:屏幕关闭后保持相机LED亮起

ste*_*ker 6 camera android led wakelock flashlight

因为我刚开始使用Android编码,所以我犹豫是否发布了我的问题,但现在我正处于无法抗拒的地步.

我有一项服务打开摄像头LED onCreate:

@Override
public void onCreate() {
// make sure we don't sleep
this.pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SleepLED");

    this.mTimer = new Timer();
    this.mTimerTask = new TimerTask() {
        public void run() {
        // turn on the LED
        setFlashlight(Camera.Parameters.FLASH_MODE_TORCH);

        mWakeLock.acquire();
        }
    };

    // Get the notification-service
    this.mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    // Display a notification about us starting.  We put an icon in the status bar.
    showNotification();

    // Open camera
    this.frontCam = Camera.open();
    this.frontCamPara = frontCam.getParameters();
    this.frontCam.lock();


    // Schedule the TimerTask
    this.mTimer.schedule(mTimerTask, 0);
}
Run Code Online (Sandbox Code Playgroud)

我可以告诉WakeLock获得了,我用FULL_WAKE_LOCK测试了它并没有关闭屏幕超时.但由于屏幕不需要打开,我不想使用完整的唤醒锁.

我的手机Cyanogenmod(HTC Legend)带来了一个可以做我想要的火炬应用程序.它的源代码在这里:
https://github.com/CyanogenMod/android_packages_apps_Torch/tree/gingerbread/src/net/cactii/flash2
我注意到那个应用程序关灯会短暂关闭,如果这是一个暗示某人,显然不适合我;(

我不希望有人改变我的代码来做我想做的事情,但如果有人能指出我正确的方向,我会感激不尽!

迎接
SJ

ste*_*ker 2

我已经找到了解决问题的方法。当手机关闭屏幕时,它会停用摄像头 LED,但它允许用户像这样重新激活它:

@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);
                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();
    }
}
Run Code Online (Sandbox Code Playgroud)

关键是将状态更改回 FLASH_MODE_OFF,然后再更改回 FLASH_MODE_TORCH。