简体   繁体   English

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

[英]Field requires API level 29 (current min is 16): android.app.TaskInfo#topActivity

I tried to check whether activity is in foreground or background, so I used activity manager to find that.我试图检查活动是在前台还是后台,所以我使用活动管理器来找到它。 When my compileSdkVersion is 28 the app is compiled successfully.当我的compileSdkVersion为 28 时,应用程序编译成功。 When I run the same code with compileSdkVersion 29, I get below error,当我使用compileSdkVersion 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;
    }

}

I get this error when I set compileSdkVersion to 29.当我将compileSdkVersion设置为 29 时出现此错误。

taskInfo.get(0).topActivity

this method added to API level 29. So below 29, you can not use this这个方法添加到API级别29。所以低于29,你不能使用这个

replace your method with this用这个替换你的方法

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android-Settings.Global#AIRPLANE_MODE_ON-字段需要API级别17(当前最小值为16) - android - Settings.Global#AIRPLANE_MODE_ON - Field requires API level 17 (current min is 16) 调用需要API级别16(当前最小值为14):android.app.Notification.Builder #build - Call requires API level 16 (current min is 14): android.app.Notification.Builder#build 需要API级别21(当前最小值为16) - Requires API level 21 (current min is 16) 调用需要 API 级别 29(当前最小值为 21):`android.widget.NumberPicker#setTextColor` - Call requires API level 29 (current min is 21): `android.widget.NumberPicker#setTextColor` 调用要求API级别16(当前最小值为14),并带有EndAction - Call requires API level 16 (current min is 14) withEndAction 呼叫需要API级别16(当前最小值为14) - Call requires API level 16 (current min is 14) 调用需要API级别16(当前最小值为10):android.widget.ImageView#getMaxWidth - Call requires API level 16 (current min is 10): android.widget.ImageView#getMaxWidth 如何在Android Studio中为消息“调用要求API级别21(当前最小值为16)”启用lint错误? - How to enable lint error in Android Studio for message “Call requires API level 21 (current min is 16)”? 错误消息:字段要求API级别11(当前最小值为8):android.os.AsyncTask#THREAD_POOL_EXECUTOR - Error message: Field requires API level 11 (current min is 8): android.os.AsyncTask#THREAD_POOL_EXECUTOR 调用需要API级别11(当前最小值为9)android.app.Activity#onCreateView - Call requires API level 11(current min is 9) android.app.Activity#onCreateView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM