如何在Android 5.0 +中获得热门活动

时间:2016-04-12 05:04:09

标签: android android-activity process

我使用以下代码获取热门活动

private String getTopPackageName() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return activityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
    } else {
        final List<ActivityManager.RunningAppProcessInfo> pis = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo pi : pis) {
            if (pi.pkgList.length == 1) return pi.pkgList[0];
        }
    }
    return "";
}

它关于API&lt; 21但是API&gt; = 21它返回&#34;&#34; :( 怎么做到的? 我会尝试:

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

}

但是返回NULL :(请帮帮我!谢谢

3 个答案:

答案 0 :(得分:1)

API级别21(棒棒糖)

中不推荐使用

getRunningTasks()

作为替代,您可以这样做

List<ActivityManager.RunningAppProcessInfo> task = manager.getRunningAppProcesses();
ComponentName componentInfo = task.get(0).importanceReasonComponent;
String packageName = componentInfo.getPackageName()

注意:

  

此方法仅用于调试和呈现任务管理用户界面。这绝不应该用于应用程序中的核心逻辑,例如根据此处的信息在不同的行为之间做出决定。

Refer the doc

更新:

    ActivityManager manager = (ActivityManager) MainActivity.this.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> task = manager.getRunningAppProcesses();
    ActivityManager.RunningAppProcessInfo runningAppProcessInfo = null;

    for(ActivityManager.RunningAppProcessInfo singleTask : task){
        if(singleTask.importanceReasonComponent !=null){
            runningAppProcessInfo = singleTask;
            break;
            }
    }

    if(runningAppProcessInfo!=null) {
        ComponentName componentInfo = runningAppProcessInfo.importanceReasonComponent;
        String packageName = componentInfo.getPackageName();
        Log.d("myPackageName", packageName);
    }

答案 1 :(得分:0)

您也可以使用辅助功能服务,但为此您需要将用户重定向到辅助功能设置以将其启用。示例代码如下所示,以获取刚刚打开的应用程序包或在您的情况下是最重要的活动。

  public class MyAccessibilityService extends AccessibilityService {

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
             String packageName = event.getPackageName();

        }
  }

答案 2 :(得分:0)

您需要对UsageStatsManager使用权限&#34; PACKAGE_USAGE_STATS&#34;

&#34;此API需要权限 android.permission.PACKAGE_USAGE_STATS ,这是系统级权限,不会授予第三方应用。但是,声明权限意味着有意使用API​​,设备用户可以通过“设置”应用程序授予权限。&#34;

参见:https://developer.android.com/reference/android/app/usage/UsageStatsManager.html