首页 > 移动平台 > 详细

android 判断app是否处于前台

时间:2016-01-28 16:58:51      阅读:217      评论:0      收藏:0      [点我收藏+]

逻辑是获取系统的运行栈,处于栈顶的就是处于前台的app,然后对比包名,不同则表示app处于后台,反之,则表示app处于前台.

/**
     * app是否在后台
     *
     * @param context
     * @return true 是 false 不是
     */
    public static boolean isBackground(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            StaticMethod.debugEMSG(topActivity.getPackageName() + " : " + context.getPackageName());
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
                return true;
            }
        }
        return false;
    }

  

android 判断app是否处于前台

原文:http://www.cnblogs.com/sun-yang-/p/5166720.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!