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.app.ActivityManager.RunningTaskInfo;

import android.content.Context;

import java.util.List;

public class Main {
    /**
     * Determine if the app is currently visible to the user
     *
     * @param context
     * @return boolean Return true if the app is visible to the user otherwise false
     */
    public static boolean isAppInForeground(Context context) {
        final List<RunningTaskInfo> task = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE))
                .getRunningTasks(1);
        if (task.isEmpty() && task.size() > 0) {
            return false;
        }
        return task.get(0).topActivity.getPackageName().equalsIgnoreCase(context.getPackageName());
    }
}