Check the current running tasks. - Android Android OS

Android examples for Android OS:Process

Description

Check the current running tasks.

Demo Code


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

public class Main {

    public static boolean isMyApplicationTaskOnTop(Context context) {
        String packageName = context.getPackageName();
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> recentTasks = activityManager
                .getRunningTasks(Integer.MAX_VALUE);
        if (recentTasks != null && recentTasks.size() > 0) {
            RunningTaskInfo t = recentTasks.get(0);
            // com.android.launcher
            String pack = t.baseActivity.getPackageName();
            if (pack.equals(packageName)) {
                return true;
            }/*  w  ww . j  a  v  a2s  . co  m*/
        }
        return false;
    }
}

Related Tutorials