利用Application监听App处于前台和后台

之前有个需求,App从前台切换到后台,不需要做什么操作;从后台切换到前台,需要先验证屏锁密码。于是乎,搜索了一下,“利用Application监听App处于前台和后台”,找到了一个国外的big god的解决方案。不要问我是怎么找到的,做开发必备的技能,擅长检索,也是一种能力。致谢steveliles

...

废话不说了,直接上代码。

<pre>

public class ForegroundCallbacks implements Application.ActivityLifecycleCallbacks {

public static final long CHECK_DELAY = 500;
public static final String TAG = ForegroundCallbacks.class.getName();

public interface Listener {

    public void onBecameForeground();

    public void onBecameBackground();

}

private static ForegroundCallbacks instance;

private boolean foreground = false, paused = true;
private Handler handler = new Handler();
private List<Listener> listeners = new CopyOnWriteArrayList<Listener>();
private Runnable check;


public static ForegroundCallbacks init(Application application){
    if (instance == null) {
        instance = new ForegroundCallbacks();
        application.registerActivityLifecycleCallbacks(instance);
    }
    return instance;
}

public static ForegroundCallbacks get(Application application){
    if (instance == null) {
        init(application);
    }
    return instance;
}

public static ForegroundCallbacks get(Context ctx){
    if (instance == null) {
        Context appCtx = ctx.getApplicationContext();
        if (appCtx instanceof Application) {
            init((Application)appCtx);
        }
        throw new IllegalStateException(
            "Foreground is not initialised and " +
            "cannot obtain the Application object");
    }
    return instance;
}

public static ForegroundCallbacks get(){
    if (instance == null) {
        throw new IllegalStateException(
            "Foreground is not initialised - invoke " +
            "at least once with parameterised init/get");
    }
    return instance;
}

public boolean isForeground(){
    return foreground;
}

public boolean isBackground(){
    return !foreground;
}

public void addListener(Listener listener){
    listeners.add(listener);
}

public void removeListener(Listener listener){
    listeners.remove(listener);
}

@Override
public void onActivityResumed(Activity activity) {
    paused = false;
    boolean wasBackground = !foreground;
    foreground = true;

    if (check != null)
        handler.removeCallbacks(check);

    if (wasBackground){
        LogUtil.e("went foreground");
        for (Listener l : listeners) {
            try {
                l.onBecameForeground();
            } catch (Exception exc) {
                LogUtil.e("Listener threw exception!", exc);
            }
        }
    } else {
        LogUtil.e("still foreground");
    }
}

@Override
public void onActivityPaused(Activity activity) {
    paused = true;

    if (check != null)
        handler.removeCallbacks(check);

    handler.postDelayed(check = new Runnable(){
        @Override
        public void run() {
            if (foreground && paused) {
                foreground = false;
                LogUtil.e("went background");
                for (Listener l : listeners) {
                    try {
                        l.onBecameBackground();
                    } catch (Exception exc) {
                        LogUtil.e("Listener threw exception!", exc);
                    }
                }
            } else {
                LogUtil.e("still foreground");
            }
        }
    }, CHECK_DELAY);
}

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

@Override
public void onActivityStarted(Activity activity) {}

@Override
public void onActivityStopped(Activity activity) {}

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

@Override
public void onActivityDestroyed(Activity activity) {}

}

</pre>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值