检查应用程序最小化/背景 [英] Check application is minimized/background

查看:94
本文介绍了检查应用程序最小化/背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶!

由于无法确定当前在前台或后台应用程序,

Because not able identify application currently in foreground or background,

当我pressed home键和我的应用程序是不是在前台。再后,我已经习惯了检查正在运行的应用程序在前台还是不行。

when i pressed home button and my application is not in foreground. then after i have used to check application running in foreground or not.

public boolean isAppOnForeground()
    {

        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null) { return false; }
        final String packageName = context.getPackageName();
        for (RunningAppProcessInfo appProcess : appProcesses)
        {
            if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) { return true; }
        }
        return false;

    }

它会总是返回真正即使我的应用程序实际上不会在前台。

it will returns always true even if my application actually not in foreground.

注意: 我的应用程序有2个服务, 当应用程序从最近的任务列表中删除1服务将被取消(,我取得的成绩。) 当应用程序进入后台2服务将被取消。 (即检查WiFi信号强度。)

Note: My application have 2 services, 1 service will be canceled when application is removed from recent task list( that i have achieved.) 2 service will be canceled when application goes in background. (that is check wifi signal strength.)

如何知道应用程序是前景或背景?

如何在应用程序是不是在前台?停止服务

推荐答案

活动回调方法,如的onPause()的onStop() onResume(),会给你提示的当前状态活动。该活动参考给每个这些方法的一个非常广泛的解释。

Rely on Activity callback methods, such as onPause(), onStop() and onResume(), that will give you hints on the current state of Activity. The Activity reference gives a very broad explanation of every of those methods.

这篇关于检查应用程序最小化/背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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