码迷,mamicode.com
首页 > 移动开发 > 详细

android 判断app是否处于前台

时间:2016-01-28 17:02:09      阅读:151      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!