is App Running Foreground - Android App

Android examples for App:App Running

Description

is App Running Foreground

Demo Code


//package com.java2s;

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

public class Main {

    public static boolean isAppRunningForeground(Context context,
            String packageName) {
        ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Activity.ACTIVITY_SERVICE);
        List<RunningTaskInfo> mRunningTaskInfoList = mActivityManager
                .getRunningTasks(1);/* w  w  w  . j  av a  2  s .c om*/
        if (mRunningTaskInfoList == null || mRunningTaskInfoList.isEmpty())
            return false;
        RunningTaskInfo mRunningTaskInfo = mRunningTaskInfoList.get(0);
        String strPackageName = mRunningTaskInfo.topActivity
                .getPackageName();
        if (strPackageName != null && strPackageName.equals(packageName)) {
            return true;
        } else {
            return false;
        }
    }
}

Related Tutorials