Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;

import android.content.Context;

public class Main {
    /**
     * is out app running in background ?
     * 
     * when system reboot, this method will return false although our app is not already running, so is this a bug?
     * 
     * @param context
     * @return true if in background; false if not.
     */
    public static boolean isInBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND
                        || appProcess.importance == RunningAppProcessInfo.IMPORTANCE_SERVICE) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return false;
    }
}