Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.app.ActivityManager;
import android.content.Context;
import java.util.List;

public class Main {
    public static boolean isApplicationForeground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
        for (ActivityManager.RunningTaskInfo info : services) {
            if (info.topActivity.getPackageName().equalsIgnoreCase(context.getPackageName())) {
                return true;
            }
        }
        return false;
    }
}