android - 字段需要 API 级别 29(当前最低为 16):android.app.TaskInfo#topActivity
问题描述
我试图检查活动是在前台还是后台,所以我使用活动管理器来找到它。当我compileSdkVersion
28 岁时,应用程序编译成功。当我用 29 运行相同的代码时compileSdkVersion
,出现以下错误,
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;
}
推荐阅读
- css - 在 div 中定位背景图像(中心、顶部等)
- pandas - 熊猫将行添加到日期时间索引数据框
- android - Flutter, video_player I/ACodec 编解码器不支持配置优先级 (err -2147483648)
- python - VS Code - 解释器的清理列表
- spring - 如何在其方法中访问此端点 @GetMapping("/catalog/**") 的 url?
- wordpress - Wordpress 帖子导入后的巨大服务器响应时间(12 秒)(约 1000 个帖子)
- python - AWS 上的 Django 性能监控
- r - ggplot2 垂直剖面图
- javascript - 使用 Vue 触发链接内的按钮
- flutter - 您可以在 http.post() 方法上删除 google sheet 行吗?