2

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. When I run the same code with compileSdkVersion 29, I get below error,

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.

Priyanka
  • 3,369
  • 1
  • 10
  • 33
Pavithra
  • 31
  • 1
  • 5

1 Answers1

2
taskInfo.get(0).topActivity

this method added to API level 29. So below 29, you can not use this

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;
}
Priyanka
  • 3,369
  • 1
  • 10
  • 33