【问题标题】:How to determine current foreground app name in Marshmallow?如何确定棉花糖中当前的前台应用程序名称?
【发布时间】:2016-07-20 19:46:21
【问题描述】:

我正在尝试识别 Marshmallow 中的当前前台应用程序名称(查看下图)。

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

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

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

packagename = componentInfo.getPackageName();

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

但似乎 getRunningTasks() 已被弃用。您能否为此建议一种替代方法。

【问题讨论】:

  • 你至少可以在这里写代码

标签: android foreground


【解决方案1】:

我找到了一个适用于 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

你可以走了。 :)

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 感谢您的回复。但我正在寻找任何其他应用程序,而不是属于我当前应用程序的应用程序。
    【解决方案3】:

    使用此方法使当前前台应用程序大部分工作

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多