Android 5.0(包含5.0以下版本) 获取栈顶应用程序包名

在此Android 5.0以下的版本就不说了,网上搜索基本全是实现方法,使用getRunningTask方法就可以获取到

我这里着重说下Android5.0以上版本获取栈顶包名的方法:

方法一:

注:该方法在获取小米Android5.0.2版本的手机栈顶包名时是获取不到的(魅族、三星、华为、酷派、vivo都可以获取到),可以使用下面的方法二来实现。鉴于手头设备不全只能测试这么几种。

/**
     * 获取程序包名(本程序包名5.0版本上下都可获取)
     * 
     * @return
     */
    public String getTaskPackname() {
        ActivityManager.RunningAppProcessInfo currentInfo = null;
        Field field = null;
        int START_TASK_TO_FRONT = 2;
        String currentApp = "CurrentNULL";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            try {
                field = ActivityManager.RunningAppProcessInfo.class.getDeclaredField("processState");
            } catch (Exception e) {
                e.printStackTrace();
            }
            ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningAppProcessInfo> appList = am.getRunningAppProcesses();
            for (ActivityManager.RunningAppProcessInfo app : appList) {
                if (app.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    Integer state = null;
                    try {
                        state = field.getInt(app);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (state != null && state == START_TASK_TO_FRONT) {
                        currentInfo = app;
                        break;
                    }
                }
            }
            if (currentInfo != null) {
                currentApp = currentInfo.processName;
            }
        } else {
            ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
            currentApp = tasks.get(0).processName;
        }
        Log.e("TAG", "Current App in foreground is: " + currentApp);
        return currentApp;
    }

方法二:

注:该方法使用 UsageStatsManager来实现获取,

UsageStatsManager


但是此方法也有其局限性:

1、UsageStatsManager类是在AndroidL以上才有的,所以导入sdk版本要在Android5.0以上

2、使用此方法需要在设备中的“有权查看使用情况的应用”模块中开启应用权限,开启方法见我的另一篇帖子Android 5.0以后版本打开“有权查看使用情况的应用”

/**
     * 获取最顶层程序包名
     * 
     * @return
     */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @SuppressLint("NewApi")
    private String getTaskPackname() {
        String currentApp = "CurrentNULL";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            UsageStatsManager usm = (UsageStatsManager) this.getSystemService(Context.USAGE_STATS_SERVICE);
            long time = System.currentTimeMillis();
            List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time);
            if (appList != null && appList.size() > 0) {
                SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
                for (UsageStats usageStats : appList) {
                    mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
                }
                if (mySortedMap != null && !mySortedMap.isEmpty()) {
                    currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
                }
            }
        } else {
            ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
            currentApp = tasks.get(0).processName;
        }
        Log.e("TAG", "Current App in foreground is: " + currentApp);
        return currentApp;
    }
    

好了以上两个方法应该可以满足Android5.0以上版本获取栈顶包名的需求了,大家如有其它的实现方法请与我联系,共同探讨一下 ^_^ ^_^


相关帖子:

http://stackoverflow.com/questions/26431795/how-to-use-usagestatsmanager

http://stackoverflow.com/questions/24625936/getrunningtasks-doesnt-work-in-android-l#

http://stackoverflow.com/questions/2166961/determining-the-current-foreground-application-from-a-background-task-or-service

http://stackoverflow.com/questions/26714285/is-there-an-alternative-for-getrunningtask-api

http://stackoverflow.com/questions/24590533/how-to-get-recent-tasks-on-android-l







你可能感兴趣的:(Android 5.0(包含5.0以下版本) 获取栈顶应用程序包名)