5

I am trying to figure out how to turn off the screen from within a service. I have already read this but I am failing to realize how to do this from within a service. I don't really want to deal with wake locks because as soon as the screen goes off, I don't really care about turning it back via java code. I just need a one time method for turning the screen off and I have searched forever on this.

Community
  • 1
  • 1
SemperGumbee
  • 474
  • 1
  • 8
  • 19

1 Answers1

3

I see two options:

(1) Create a dummy Activity and use it to get a Window object through the getWindow(); method. Then you would use an Intent to call the screen off from your Service.

(2) Use IHardwareService.Stub. Not part of the SDK, but there's a workaround in this blog post: https://web.archive.org/web/20210224163252/http://www.tutorialforandroid.com/2009/01/changing-screen-brightness.html

BTW: I would strongly recommend the first option. You never know when a class that is not part of the SDK might change.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • By dummy activity, do you mean just create an activity and don't inflate with XML? Is that possible? – SemperGumbee May 07 '11 at 02:17
  • @Semper yes. But thinking it better, if you only care to turn off the screen, a completely black activity should do the same trick. That is, of course, unless you want to turn off the *device*, but that's another issue. – Aleadam May 07 '11 at 06:40
  • 1
    I'll look into it tomorrow and get back with you on this. Whowoudaevarthunk...black activity :) – SemperGumbee May 07 '11 at 07:07
  • So, I got it working. I created a transparent activity and scaled the brightness down to 0 in 3 seconds since that is the fastest timeout you can set. Is there any way to overcome this without having to be signed as a system app? It technically works, but I need instant off, not a fadeout in 3 seconds. 3 seconds is just a tad bit too long. – SemperGumbee May 09 '11 at 07:01
  • is it possible to wake the screen up using the above approach? – Bear May 27 '11 at 15:29
  • Actually the second approach (IHardwareService) is not resultful in Android 2.2+. So the 1st way is recommended. – Richard Ye Jan 11 '12 at 05:41