2

I have an app that needs to do some background cleaning job when it goes idle, it uses a service that's always running in the background and a BroadcastReceiver registered on ACTION_SCREEN_OFF and ACTION_SCREEN_ON which detects when the screen goes off and runs the background worker, also I'm using the onPause method in my main activity to signal the service to start the background worker.

The problem I'm having is that when the screen switches off the onPause is been called first then the BroadcastReceived detects the screen off action, so the background worker gets called twice.

Is it possible to find in the onPause method if the app is minimized and the screen is still on?

razz
  • 9,770
  • 7
  • 50
  • 68
  • Just have the service check if the worker is already running before starting a new one? – JimmyB Feb 11 '16 at 13:36
  • @HannoBinder that could work but i'd prefer checking if the screen is on rather that using global booleans, unless this is the only way to do that. – razz Feb 11 '16 at 13:43

1 Answers1

0

This question is similar to some of questions. I am attaching their links.

how to check screen on/off status in onStop()?

How can I tell if the screen is on in android?

Community
  • 1
  • 1