user1974977
user1974977

Reputation: 53

Android receive screen off/on events when application is not running

I've registered the Intent.action_screen_off/on in my oncreate method of my application, and i can receive the events when my application is running. But when my application exits i get an broadcast intent leaked exception (off the top of my head i think thats what its called, either way its caused by not unregistering my receiver when my app exits)

How can i receive these events when my application is not running? i'd prefer not to use a service or something like that, i've seen other apps do it without services or background processes.

Upvotes: 0

Views: 349

Answers (1)

Squonk
Squonk

Reputation: 48871

i've seen other apps do it without services or background processes.

That's not possible. The only way to register a BroadcastReceiver other than in running code is to declare it in the AndroidManifest.xml with the relevant <intent-filter>. As it's not possible to do that for either Intent.ACTION_SCREEN_OFF or Intent.ACTION_SCREEN_ON, the apps you've seen MUST have some running component(s) in order to receive the broadcasts.

In short, use a Service - I'm not quite sure why you say you'd prefer not to.

Upvotes: 2

Related Questions