gpt4 book ai didi

android - 字段需要 API 级别 29(当前最小值为 16): android. app.TaskInfo#topActivity

转载 作者:行者123 更新时间:2023-12-04 10:32:38 32 4
gpt4 key购买 nike

我试图检查 Activity 是在前台还是后台,所以我使用 Activity 管理器来找到它。
当我的 compileSdkVersion是 28 应用程序编译成功。当我使用 compileSdkVersion 运行相同的代码时29,我得到以下错误,

The field requires API level 29 (current min is 16): android.app.TaskInfo#topActivity


import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class ActivityState {
private Timer timer;
Context cntx;
Session session;

public boolean isAppIsInBackground(Context context) {
cntx = context;
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;
}
}
}
}
} else {
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity; *// this line shows error as "Field requires API level 29 (current min is 16): android.app.TaskInfo#topActivity"*
if (componentInfo.getPackageName().equals(context.getPackageName())) {
isInBackground = false;
}
}

return isInBackground;
}

}

设置 compileSdkVersion 时出现此错误到 29。

最佳答案

taskInfo.get(0).topActivity

这个方法添加到API级别29。所以低于29,你不能使用这个

用这个替换你的方法
public boolean isAppIsInBackground(Context context) {
cntx = context;
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = null; // this line shows error as "Field requires API level 29 (current min is 16): android.app.TaskInfo#topActivity"*
componentInfo = taskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equals(context.getPackageName())) {
isInBackground = false;
}
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;
}
}
}
}
}

return isInBackground;
}

关于android - 字段需要 API 级别 29(当前最小值为 16): android. app.TaskInfo#topActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60338401/

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