java 获取前景应用程序(Dep)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 获取前景应用程序(Dep)相关的知识,希望对你有一定的参考价值。

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public String getForegroundApp(Context context) {
        String currentApp = "NULL";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            try {
                Class.forName( "android.app.usage.UsageStatsManager" );
                UsageStatsManager usm = (UsageStatsManager) context.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();
                    }
                }
            } catch( ClassNotFoundException e ) {
                //my class isn't there!
                Log.i (TAG, "Class not found: android.app.usage.UsageStatsManager");
            }
        } else {
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
            currentApp = tasks.get(0).processName;
        }

        Log.i("Foreground App", "getForegroundApp: " + currentApp);
        return currentApp;
    }

以上是关于java 获取前景应用程序(Dep)的主要内容,如果未能解决你的问题,请参考以下文章

.NET和Java的应用和前景

java前景

java前景怎么样?就业领域都有哪些

Java职业发展前景

stanford nlp 3.8.0 parse中通过java程序获取root节点

Java开发就业前景及发展空间如何?