The following answer: “Is application running in background“, summarizes solutions available for background/foreground checking.
Note:
Previously this answer suggested to use ActivityManager.getRunningAppProcesses()
, however that method appeared to be not completely reliable and its usage is discouraged. Check the link above for the details.