【问题标题】:Field requires API level 29 (current min is 16): android.app.TaskInfo#topActivity字段需要 API 级别 29(当前最低为 16):android.app.TaskInfo#topActivity
【发布时间】:2020-02-21 12:10:01
【问题描述】:

我试图检查活动是在前台还是后台,所以我使用活动管理器来找到它。 当我的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 时出现此错误。

【问题讨论】:

    标签: android android-activitymanager activity-state


    【解决方案1】:
    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多