6

since i'm at the beginning with Android coding i hesitated to post my question, but now i'm at the point where i can't resist.

I have a service which turns on the camera-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);
}

I can tell that the WakeLock acquires, I tested with FULL_WAKE_LOCK and it didn't turn off considering Screen Time-Out. But since the Screen isn't need to be on I don't want to use the full wakelock.

The Cyanogenmod for my phone (HTC Legend) brings an torch-app which can do what i want. Its source code is here:
https://github.com/CyanogenMod/android_packages_apps_Torch/tree/gingerbread/src/net/cactii/flash2
I noticed that the light turns off for a short moment with that app, if this is a hint for someone, obviously not for me ;(

I don't expect someone to change my code to do what i want but I'd be thankful if anyone could point me in the right direction!

Greets
SJ

4

3 回答 3

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();
    }
}

关键是将状态更改回 FLASH_MODE_OFF,而不是更改回 FLASH_MODE_TORCH。

于 2011-11-17T21:06:40.407 回答
1

我不知道 HTC 是否属于这个阵营,但有几款设备(尤其是 Moto Droid)在屏幕关闭时无法使用摄像头——它们很有帮助地假设您永远不想以这种方式使用手机。如果屏幕锁定,使用相机的应用程序将进入后台并停止运行。

结果是我见过的安全摄像头/网络摄像头应用程序具有使屏幕变暗/变黑的功能,但它们实际上并不允许它锁定。这可确保相机继续运行。

我怀疑由于 LED 被认为是相机硬件的一部分,它面临着同样的限制。

于 2011-11-16T16:52:45.663 回答
0

我做了一些与 stefanjunkers 类似的事情,但在让它工作时遇到了问题,灯有时会重新亮起并一直亮着,有时又重新亮起,然后又很快又熄灭了。经过一番摆弄后,我发现暂停线程一会儿解决了它:

setFlashlight(Camera.Parameters.FLASH_MODE_OFF);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
setFlashlight(Camera.Parameters.FLASH_MODE_TORCH);
于 2014-01-14T13:30:25.280 回答