gpt4 book ai didi

Android 5.0+ getRunningTasks 已弃用

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:21:51 24 4
gpt4 key购买 nike

我已经完成了这个question还有这个question .但是借助这个 library我现在可以使用以下代码获取前台任务列表。

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { //For versions less than lollipop
ActivityManager am = ((ActivityManager) getSystemService(ACTIVITY_SERVICE));
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(5);
top = taskInfo.get(0).topActivity.getPackageName();
Log.v(TAG, "top app = " + top);
}else{ //For versions Lollipop and above
List<AndroidAppProcess> processes = ProcessManager.getRunningForegroundApps(getApplicationContext());
Collections.sort(processes, new ProcessManager.ProcessComparator());
for (AndroidAppProcess process : processes) {
if (process.foreground) {
top =process.name;
Log.v(TAG,top);
}
}
}

在这里,对于 Android 5.0+,我得到了所有正在运行的前台进程,但我无法断定哪个应用是顶级应用。

以上代码的输出(对于其他条件)

com.android.vending
com.google.android.gms
com.google.android.googlequicksearchbox
com.google.android.videos
com.test1
com.naag.testing
com.example.android.gettask

我的顶级应用是 com.google.android.videos

现在如何以编程方式确定 com.google.android.videos 是上述列表中的顶级应用程序?

applocker(或类似 applocker)应用程序如何在 5.0+ 上运行?希望有人帮助这对某人有帮助。

最佳答案

所以这是一个更新。在 5.0 和 5.1.1 设备中测试。完美运行。

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { //For versions less than lollipop
ActivityManager am = ((ActivityManager) getSystemService(ACTIVITY_SERVICE));
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(5);
top = taskInfo.get(0).topActivity.getPackageName();
Log.v(TAG, "top app = " + top);
}else{ //For versions Lollipop and above
List<AndroidAppProcess> processes = ProcessManager.getRunningForegroundApps(getApplicationContext());
Collections.sort(processes, new ProcessManager.ProcessComparator());
for (int i = 0; i <=processes.size()-1 ; i++) {
if(processes.get(i).name.equalsIgnoreCase("com.google.android.gms")) { //always the package name above/below this package is the top app
if ((i+1)<=processes.size()-1) { //If processes.get(i+1) available, then that app is the top app
top = processes.get(i + 1).name;
} else if (i!=0) { //If the last package name is "com.google.android.gms" then the package name above this is the top app
top = processes.get(i - 1).name;
} else{
if (i == processes.size()-1) { //If only one package name available
top = processes.get(i).name;
}
}
Log.v(TAG, "top app = " + top);
}
}
}

多亏了这个library

现在我可以在 Android 5.0+ 中获取前台任务

关于Android 5.0+ getRunningTasks 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33918528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com