Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 This file is part of MobileLib, a free-software/open source library
 for mobile app development.
    
 MobileLib is free software: you can redistribute it and/or modify it
 under the terms of the MobileLib license.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the license for more details.
 */

import android.app.ActivityManager;
import android.content.Context;
import java.util.List;

public class Main {
    static public boolean isAppInForeground(Context context) {
        List<ActivityManager.RunningTaskInfo> tasks = ((ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1);
        if (tasks.isEmpty()) {
            return false;
        }
        return tasks.get(0).topActivity.getPackageName().equalsIgnoreCase(context.getPackageName());
    }
}