Question

I use certain sounds to notify the user of certain events. However, I do not want the app to notify the user with sounds if the activity is not on foreground.

How can I check if the app is running on background or not?

Was it helpful?

Solution

Expanding on mco's answer, your GCMIntentService needs to do something that will trigger work in a foreground activity of yours, if you have a foreground activity. Typically, you will do this by setting up your activities to respond to some sort of message in onResume() and removing that in onPause().

"Some sort of message" could be:

  • An Intent sent via LocalBroadcastManager
  • An Intent sent via the classic sendBroadcast()
  • A message via a message bus, like Otto
  • Etc.

OTHER TIPS

In the Activity cycle, onResume is called when the app becomes visible (foreground) and onPause is called when it is not visible (background).

You can use these functions to do whatever you want to do when the app is background/foreground.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top