首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何判断Android应用是否在前台运行?

如何判断Android应用是否在前台运行?
EN

Stack Overflow用户
提问于 2011-04-01 02:36:33
回答 14查看 79.1K关注 0票数 83

我正在我的安卓应用程序中执行由c2dm触发的状态栏通知。如果应用程序正在运行,我不想显示通知。如何确定应用程序是否正在运行以及是否处于前台?

EN

回答 14

Stack Overflow用户

回答已采纳

发布于 2011-04-01 02:43:53

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

示例代码:

代码语言:javascript
复制
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;
}
票数 55
EN

Stack Overflow用户

发布于 2014-10-31 04:27:42

这是一个相当古老的帖子,但仍然非常相关。上述被接受的解决方案可能有效,但是错误的。As Dianne Hackborn wrote:

这些API不是应用程序的UI流的基础,而是做一些事情,比如向用户显示正在运行的应用程序,或者任务管理器,等等。

是的,在内存中有一个关于这些东西的列表。然而,它是在另一个进程中关闭的,由独立于您的进程运行的线程管理,并且您不能指望(a)及时看到做出正确的决定或(b)在您返回时有一个一致的画面。另外,关于“下一个”活动的决定总是在发生切换的时候完成,直到那个确切的点(活动状态被短暂锁定以进行切换),我们才真正知道下一件事将是什么。

这里的实现和全局行为不能保证在未来保持不变。

正确的解决方案是实现:ActivityLifeCycleCallbacks

这基本上需要一个应用程序类,可以在其中设置处理程序来识别应用程序中活动的状态。

票数 46
EN

Stack Overflow用户

发布于 2016-01-15 01:46:27

正如维奈所说,可能最好的解决方案(支持较新的安卓版本,14+)是在Application类实现中使用ActivityLifecycleCallbacks

代码语言:javascript
复制
package com.telcel.contenedor.appdelegate;

import android.app.Activity;
import android.app.Application.ActivityLifecycleCallbacks;
import android.os.Bundle;

/** Determines global app lifecycle states. 
 * 
 * The following is the reference of activities states:
 * 
 * The <b>visible</b> lifetime of an activity happens between a call to onStart()
 * until a corresponding call to onStop(). During this time the user can see the
 * activity on-screen, though it may not be in the foreground and interacting with 
 * the user. The onStart() and onStop() methods can be called multiple times, as 
 * the activity becomes visible and hidden to the user.
 * 
 * The <b>foreground</b> lifetime of an activity happens between a call to onResume()
 * until a corresponding call to onPause(). During this time the activity is in front
 * of all other activities and interacting with the user. An activity can frequently
 * go between the resumed and paused states -- for example when the device goes to
 * sleep, when an activity result is delivered, when a new intent is delivered -- 
 * so the code in these methods should be fairly lightweight. 
 * 
 * */
public class ApplicationLifecycleManager implements ActivityLifecycleCallbacks {

    /** Manages the state of opened vs closed activities, should be 0 or 1. 
     * It will be 2 if this value is checked between activity B onStart() and
     * activity A onStop().
     * It could be greater if the top activities are not fullscreen or have
     * transparent backgrounds.
     */
    private static int visibleActivityCount = 0;

    /** Manages the state of opened vs closed activities, should be 0 or 1
     * because only one can be in foreground at a time. It will be 2 if this 
     * value is checked between activity B onResume() and activity A onPause().
     */
    private static int foregroundActivityCount = 0;

    /** Returns true if app has foreground */
    public static boolean isAppInForeground(){
        return foregroundActivityCount > 0;
    }

    /** Returns true if any activity of app is visible (or device is sleep when
     * an activity was visible) */
    public static boolean isAppVisible(){
        return visibleActivityCount > 0;
    }

    public void onActivityCreated(Activity activity, Bundle bundle) {
    }

    public void onActivityDestroyed(Activity activity) {
    }

    public void onActivityResumed(Activity activity) {
        foregroundActivityCount ++;
    }

    public void onActivityPaused(Activity activity) {
        foregroundActivityCount --;
    }


    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
    }

    public void onActivityStarted(Activity activity) {
        visibleActivityCount ++;
    }

    public void onActivityStopped(Activity activity) {
        visibleActivityCount --;
    }
}

在应用程序onCreate()方法中:

代码语言:javascript
复制
registerActivityLifecycleCallbacks(new ApplicationLifecycleManager());

然后,将使用ApplicationLifecycleManager.isAppVisible()ApplicationLifecycleManager.isAppInForeground()来了解所需的状态。

票数 24
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5504632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档