如何判断 Android 应用程序是否在前台运行? [英] How can I tell if Android app is running in the foreground?

查看:26
本文介绍了如何判断 Android 应用程序是否在前台运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的 android 应用程序中执行由 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?

推荐答案

创建一个全局变量,如 private boolean mIsInForegroundMode; 并在 onPause 中分配一个 false 值()onResume() 中的 true 值.

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

示例代码:

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天全站免登陆