Andrew Thomas Andrew Thomas - 1 year ago 80
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?

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download