关闭

Android 如何获取最顶端的Top Activity

10668人阅读 评论(1) 收藏 举报

Android 如何获取最顶端的Top Activity, 见如下代码:

    public static class TopActivityInfo {
        public String packageName = "";
        public String topActivityName = "";
    }
    private TopActivityInfo getTopActivityInfo() {
        ActivityManager manager = ((ActivityManager)GlobalConfig.getContext().getSystemService(Context.ACTIVITY_SERVICE));
        TopActivityInfo info = new TopActivityInfo();
        if (Build.VERSION.SDK_INT >= 21) {
            List<ActivityManager.RunningAppProcessInfo> pis = manager.getRunningAppProcesses();
            ActivityManager.RunningAppProcessInfo topAppProcess = pis.get(0);
            if (topAppProcess != null && topAppProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                info.packageName = topAppProcess.processName;
                info.topActivityName = "";
            }
        } else {
            //getRunningTasks() is deprecated since API Level 21 (Android 5.0)
            List localList = manager.getRunningTasks(1);
            ActivityManager.RunningTaskInfo localRunningTaskInfo = (ActivityManager.RunningTaskInfo)localList.get(0);
            info.packageName = localRunningTaskInfo.topActivity.getPackageName();
            info.topActivityName = localRunningTaskInfo.topActivity.getClassName();
        }
        return info;
    }

我在另外一篇文章里面介绍了另外几种获取最顶端Top Activity的方法, 详见:http://blog.csdn.net/wangbaochu/article/details/50554406

2
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:359849次
    • 积分:4956
    • 等级:
    • 排名:第5688名
    • 原创:137篇
    • 转载:54篇
    • 译文:0篇
    • 评论:77条
    文章分类
    最新评论