如何判断app在前台还是后台

项目遇到一个轮训,有通知就toast。但是toast是可以不受actvity控制的,当我按home键让app常驻后台后,我要他不吐司,这时候我们需要判断app在前台还是后台。
发挥谷歌搜索的威力 Checking if an Android application is running in the background (或者简单的几个关键字)

binggo–>http://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background

做程序员就是要学会从stackoverflow抄代码

高票回答是集成在application两个static 方法,然后每个activity继承。当然未尝不可,但是这个代码量增加了。这是疑问难道谷歌没有自带的判断方法吗,当然有。第二个回答更加中肯

ActivityLifecycleCallbacks

The key is using ActivityLifecycleCallbacks (note that this requires Android API level 14 (Android 4.0)). Just check if the number of stopped activities is equal to the number of started activities. If they’re equal, your application is being backgrounded. If there are more started activities, your application is still visible. If there are more resumed than paused activities, your application is not only visible, but it’s also in the foreground. There are 3 main states that your activity can be in, then: visible and in the foreground, visible but not in the foreground, and not visible and not in the foreground (i.e. in the background).

The really nice thing about this method is that it doesn’t have the asynchronous issues getRunningTasks() does, but you also don’t have to modify every Activity in your application to set/unset something in onResumed()/onPaused(). It’s just a few lines of code that’s self contained, and it works throughout your whole application. Plus, there are no funky permissions required either.

此方法在4.0上有效,现在的手机应该没4.0以下的了吧(这么反人类?)。
public class MyLifecycleHandler implements ActivityLifecycleCallbacks {

 public static MyLifecycleHandler getInstance()   {
        if (INSTANCE == null)
            INSTANCE = new MyLifecycleHandler();
        return INSTANCE;
    }
    // I use four separate variables here. You can, of course, just use two and
    // increment/decrement them instead of using four and incrementing them all.
    private int resumed;
    private int paused;
    private int started;
    private int stopped;

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
    }

    @Override
    public void onActivityResumed(Activity activity) {
        ++resumed;
    }

    @Override
    public void onActivityPaused(Activity activity) {
        ++paused;
        android.util.Log.w("test", "application is in foreground: " + (resumed > paused));
    }

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

    @Override
    public void onActivityStarted(Activity activity) {
        ++started;
    }

    @Override
    public void onActivityStopped(Activity activity) {
        ++stopped;
        android.util.Log.w("test", "application is visible: " + (started > stopped));
    }

    // If you want a static function you can use to check if your application is
    // foreground/background, you can use the following:

    // Replace the four variables above with these four
    private static int resumed;
    private static int paused;
    private static int started;
    private static int stopped;

    // And these two public static functions
    public static boolean isApplicationVisible() {
        return started > stopped;
    }

    public static boolean isApplicationInForeground() {
        return resumed > paused;
    }

}

有兴趣的可以详细了解actvity的生命周期。4个静态变量

 private static int resumed;
    private static int paused;
    private static int started;
    private static int stopped;

通过静态方法判断

public static boolean    isApplicationVisible() {
        return started > stopped;
    }

当然啦这个方法实现就是在app注册一次

// Don't forget to add it to your manifest by doing
// <application android:name="your.package.MyApplication" ...
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        // Simply add the handler, and that's it! No need to add any code
        // to every activity. Everything is contained in MyLifecycleHandler
        // with just a few lines of code. Now *that's* nice.
        registerActivityLifecycleCallbacks(MyLifecycleHandler.getInstance());
    }
}

四级英语都可以轻松理解了。

在我们需要吐司的时候加个判断,例如

 public void showFast(CharSequence text) {
    if         (MyLifecycleHandler.getInstance().isApplicationVisible())
        show(text, 1000);
    }

最后大功告成。轻松利用静态方法就可以判断app位于前台后台。。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值