美文网首页
关于android 7.2获取顶层应用的问题

关于android 7.2获取顶层应用的问题

作者: Ed_Lannister | 来源:发表于2018-08-29 15:05 被阅读21次

    目前试了三种方法,由于21版本之前的sdk好多接口废弃了,在android7.2上getRunningTasks的方法和getRunningAppProcesses的方法都不太起作用。试到最后一种才生效。

    private String focusedPackage(){
            Log.d(TAG, "focusedPackage");
            ActivityManager am = (ActivityManager) mContext.getSystemService(mContext.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> runningTaskInfos = am.getRunningTasks(6);
            if(runningTaskInfos==null||runningTaskInfos.size()<=0){
                Log.e(TAG,"focusedPackage falied for unknown package runningTaskInfos==null");
                return null;
            }
            ComponentName cn = runningTaskInfos.get(0).topActivity;
            if(cn==null){
                Log.e(TAG,"focusedPackage falied for unknown package cn==null");
                return null;
            }
            Log.e(TAG,"focusedPackage getPackageName here" + cn.getPackageName());
            return cn.getPackageName();
        }
    
        public static class TopActivityInfo {
            public String packageName = "";
            public String topActivityName = "";
        }
    
        private TopActivityInfo getTopActivityInfo() {
            Log.e(TAG,"focusedPackage getTopActivityInfo here");
            ActivityManager manager = ((ActivityManager)mContext.getSystemService(mContext.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 = "";
                    Log.e(TAG,"focusedPackage info.packageName here 22" + info.packageName);
                }
            } 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();
                Log.e(TAG,"focusedPackage info.packageName here 21" + info.packageName);
            }
            return info;
        }
    

    虽然getRecentTasks在android studio中显示也在21之后的版本被废弃掉了,但是在target版本为25上还是实测起作用的。

        private boolean isSettingTop(){
            ActivityManager activityManager = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
            assert activityManager != null;
            String topPackage = activityManager.getRecentTasks(1,activityManager.RECENT_IGNORE_UNAVAILABLE).get(0).topActivity.getPackageName();
            Log.e(TAG,"focusedPackage isSettingTop" + topPackage);
            return SETTINGSNAME.equalsIgnoreCase(topPackage);
        }
    

    相关文章

      网友评论

          本文标题:关于android 7.2获取顶层应用的问题

          本文链接:https://www.haomeiwen.com/subject/ynxkwftx.html