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!

Community
  • 1
  • 1
The WebMacheter
  • 1,756
  • 1
  • 20
  • 31
  • Do you succeded in this requirement ? – Gajini Aug 30 '13 at 07:31
  • 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. – The WebMacheter Aug 30 '13 at 14:55
  • 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. – Shikhar Mar 21 '14 at 12:47
  • 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. – The WebMacheter Mar 21 '14 at 23:08

2 Answers2

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);
icyerasor
  • 4,305
  • 1
  • 35
  • 45
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.

Dre
  • 4,121
  • 27
  • 39
  • 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. – icyerasor Mar 06 '11 at 23:09
  • 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. – Dre Mar 06 '11 at 23:12
  • Ah okay. Then he should .acquire(), setBrightnesTo100(); .release(), right? – icyerasor Mar 06 '11 at 23:15
  • thanks! I did what you guys suggested, but it still doesn't work. I edited the question with more info, if that helps :) – The WebMacheter Mar 11 '11 at 03:23