Andrew Thomas Andrew Thomas - 4 months ago 5
Android Question

How can I tell if Android app is running in the foreground?

I am doing a status bar notification in my android app that is triggered by c2dm. I don't want to display the notification if the app is running. How do you determine if the app is running and is in the foreground?


Make a global variable like private boolean mIsInForegroundMode; and assign a false value in onPause() and a true value in onResume().

Sample code:

private boolean mIsInForegroundMode;

protected void onPause() {
    mIsInForegroundMode = false;

protected void onResume() {
    mIsInForegroundMode = true;

// Some function.
public boolean isInForeground() {
    return mIsInForegroundMode;