Android: keep the camera on the LED after turning off the screen

since I am at the beginning with Android coding, I did not dare to post my question, but now I am at a point where I can not resist.

I have a service that turns onCreate camera LED:

@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 say that WakeLock is acquiring, I tested FULL_WAKE_LOCK, and it didn’t disconnect, given Time Time Out. But since the screen is not needed, I do not want to use the full wakelock.

Cyanogenmod for my phone (HTC Legend) brings a torch application that 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 time with this application, if this is a hint for someone, obviously not for me; (

I don’t expect anyone to change my code to do what I want, but I would be grateful if someone could point me in the right direction!

Congratulated
Sj

+6
source share
3 answers

I found a solution to the problem. When the phone turns off the screen, it turns off the camera LED, but allows the user to reactivate it as follows:

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

The key changes state to FLASH_MODE_OFF and back to FLASH_MODE_TORCH.

+2
source

I don’t know if HTC is in this camp, but several devices (especially Moto Droid) cannot use the camera when the screen is off - they readily assume that you will never want to use your phone in this way. If the screen is locked, the application using the camera is placed in the background and stops working.

As a result, the applications for the security camera / webcam that I saw have a smoothing / black screen function, but in reality they do not allow blocking. This ensures that the camera continues to work.

I suspect that since the LED is considered part of the camera's hardware, it faces the same limitations.

+1
source

I did something similar to stefanjunkers, but I had problems with his work, sometimes the light returned and remained, and sometimes returned, and then quickly turned off. After a little grumbling, I found that a pause on the thread for a moment solved him:

 setFlashlight(Camera.Parameters.FLASH_MODE_OFF); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } setFlashlight(Camera.Parameters.FLASH_MODE_TORCH); 
0
source

Source: https://habr.com/ru/post/901057/


All Articles