我怎样才能知道,如果Android应用程序在前台运行? [英] How can I tell if Android app is running in the foreground?

查看:120
本文介绍了我怎样才能知道,如果Android应用程序在前台运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个状态栏通知,在我的Andr​​oid应用程序由C2DM触发。我不想,如果应用程序正在运行,显示通知。你如何确定应用程序正在运行,并在前台?

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?

推荐答案

请像私人布尔mIsInForegroundMode一个全局变量; 并分配价值的onPause()价值 onResume( )

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

样品code:

private boolean mIsInForegroundMode;

@Override
protected void onPause() {
    super.onPause();
    mIsInForegroundMode = false;
}

@Override
protected void onResume() {
    super.onResume();
    mIsInForegroundMode = true;
}

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

这篇关于我怎样才能知道,如果Android应用程序在前台运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆