Android:屏幕关闭后保持相机 LED 亮起 [英] Android: Keep camera-LED on after screen turns off

查看:24
本文介绍了Android:屏幕关闭后保持相机 LED 亮起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我有一项服务可以打开相机 LED onCreate:

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

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

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.

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

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!

问候
SJ

推荐答案

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

I have found a solution to the problem. When the phone turns off the screen it does deactivate the camera LED, but it allows a user to reactivate it like this:

@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.

The key is changing the state back to FLASH_MODE_OFF and than back to FLASH_MODE_TORCH.

这篇关于Android:屏幕关闭后保持相机 LED 亮起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆