turn the screen on/off in Android with a shake
Asked Answered
A

2

2

I'm making an app that needs to toggle the screen on/off when the user shakes the phone. So far, I've got a SensorEventListener that listens to the shakes as suggested in the answer to this question.

When I detect a shake, I change the value of the screen's brightness as suggested in this question's answer. It all works great if I don't actually turn the screen off... if I set the brightness to 0.01f through the public void setBright(float value) method it works perfectly. However, if I set the brightness to 0.0f, the phone won't turn the screen again... not until I press the power button, at least.

Is what I'm trying to do possible? what am I doing wrong?

-- EDIT --

Thanks to Dre and icyerasor I've looked further into the issue. I acquire a PARTIAL_WAKE_LOCK before I set the brightness to 0.0f, but it still doesn't turn on when I shake the phone. However, when I debug the app I see that the setBright(1.0f) gets called allright when I shake the phone with the screen turned off; My suspicion is that the lockscreen is somehow messing with it, since it kicks in when I press the power button. After I press the power button, the app continues to work as it usually does. Is there a way to bypass the lockscreen?

Thanks for your help!

Advancement answered 6/3, 2011 at 22:47 Comment(4)
Do you succeded in this requirement ?Watt
Sorry, my requirements changed and it was no longer an issue for my project. My last was my best guess: if you deactivate the lockscreen, it would probably work.Advancement
I am having the similar problem as yours. But I want to know that how you are detecting the shake event of the device when the screen is off.Austere
I haven't looked into this for a long time now, but as I recall it, I simply used a SensorEventListener and followed the suggestions provided in the answers.Advancement
C
1

Just a guess: Setting it to brighnes 0.0 might also put the phone in sleep mode?

When you want to turn it on again programmatically, try acquiring a ACQUIRE_CAUSES_WAKEUP Wakelock:

PowerManager pm = (PowerManager)mContext.getSystemService(
                                          Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
                                      PowerManager.SCREEN_BRIGHT_WAKE_LOCK
                                      | PowerManager.ACQUIRE_CAUSES_WAKEUP,
                                      TAG);
wl.acquire(1000);
Carotenoid answered 6/3, 2011 at 22:59 Comment(0)
P
1

I agree with icyerasor's guess, however -- If the guess is correct and the phone is going to sleep you will have to acquire a PARTIAL_WAKE_LOCK to keep the CPU awake before you set the brightness to 0.0

I would test this before answering but I don't have access to an Android device at this moment.

Principium answered 6/3, 2011 at 23:5 Comment(4)
I would even recommend a SCREEN_BRIGHT_WAKE_LOCK. The ACQUIRE_CAUSES_WAKEUP is only an additional modfier. And is ignored when using PARTIAL_WAKE_LOCK(at least the doc you link says that. Oh just saw that i copied the code without modyfing the flags.. editing.Carotenoid
Yeah, but holding a SCREEN_BRIGHT_WAKE_LOCK then trying to set the brightness to 0 will probably fail, the problem is the CPU is probably going to sleep and the OP's code stops running. They need to hold the PARTIAL_WAKE_LOCK to keep the CPU awake and their code running.Principium
Ah okay. Then he should .acquire(), setBrightnesTo100(); .release(), right?Carotenoid
thanks! I did what you guys suggested, but it still doesn't work. I edited the question with more info, if that helps :)Advancement

© 2022 - 2024 — McMap. All rights reserved.