android - 如何在 Android 5.0 + 中获取 Top Activity

标签 android android-activity process

我正在使用以下代码获取热门 Activity

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<21 上有字,但 API>=21 它返回“”:( 如何得到它? 我会尝试:

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 :( 请帮助我!谢谢

最佳答案

getRunningTasks()API 级别 21(Lollipop)

中已弃用

作为替代,你可以这样做

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

注意:

this method is only intended for debugging and presenting task management user interfaces. This should never be used for core logic in an application, such as deciding between different behaviors based on the information found here.

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

关于android - 如何在 Android 5.0 + 中获取 Top Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36563994/

相关文章:

android - Android 模拟器无法访问互联网,共享互联网连接

java - 无法更改 Android Activity

java - 如何在android中从 'outside'强制更新主要 Activity ?

java - 正确从 C# 运行 Java 应用程序

c - 服务器设计模式/最佳实践的最佳来源是什么?

android - 如何在 Qt/QML for android 中创建网页缩略图

android - 始终启动主要 Activity

java - 具有多个 Activity 的 Android 共享首选项

c# - C#中如何将参数传递给另一个进程

java - Imageview 不采用卡片 View 的角半径