Search code examples
androidserviceandroid-activityscreen

Android: Turn off screen from a service


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.


Solution

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