如何在Marshmallow中确定当前的前台应用程序名称?

时间:2016-04-01 05:47:30

标签: android foreground

我正在尝试识别Marshmallow中的当前前景应用程序名称(请参见下图)。

enter image description here

==========================================

ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);

List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;

packagename = componentInfo.getPackageName();

==========================================

但似乎已弃用getRunningTasks()。你能否为此提出另一种方法。

3 个答案:

答案 0 :(得分:0)

您可以将am.getAppTasks().get(0).getTaskInfo().topActivity用于当前的应用。 如果您想要进程级别信息,如进程名称,那么您可以使用 am.getRunningAppProcesses().get(0).processName

答案 1 :(得分:0)

使用此方法使当前的前台应用程序主要工作

private void printForegroundTask() {

String currentApp = "NULL";
    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);
}

答案 2 :(得分:0)

我找到了一个适用于Kitkat版本及更高版本的解决方案,该解决方案已经过我的测试。 (我没有在4.4之前测试过)。

1。。声明清单文件中的权限:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission
        android:name="android.permission.PACKAGE_USAGE_STATS"
        tools:ignore="ProtectedPermissions"/>

2。。创建前台服务。

a。:在onCreate of Service中:

@Override
    public void onCreate() {
        super.onCreate();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForeground();
        }

        TimerTask doAsynchronousTask = new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    public void run() {

                        printForegroundTask();
                   }
                });
            }

        };
        //Starts after 0 sec and will repeat on every 5 sec of time interval.
        timer.schedule(doAsynchronousTask, 0, 5000);

b。。该方法将打印前台应用程序包名称的日志:

private void printForegroundTask() {

        String currentApp = "NULL";
        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<>();
                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);
    }

3。。安装应用后。获取您的应用程序的使用权限权限: See this image

你很好。 :)