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?

有帮助吗?

解决方案

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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top