get Running Activity Count - Android Activity

Android examples for Activity:Activity Status

Description

get Running Activity Count

Demo Code


//package com.java2s;
import android.app.ActivityManager;

import android.app.ActivityManager.RunningTaskInfo;

import android.content.Context;

import java.util.Iterator;
import java.util.List;

public class Main {
    public static int getRunningActivityCount(Context context) {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> runningTaskList = activityManager
                .getRunningTasks(1);/*  ww w .j a va 2s  . c o  m*/

        int count = 0;

        for (Iterator<RunningTaskInfo> iterator = runningTaskList
                .iterator(); iterator.hasNext();) {
            RunningTaskInfo info = (RunningTaskInfo) iterator.next();
            if (info.baseActivity.getPackageName().equals(
                    context.getPackageName())) {
                count = info.numActivities;
            }
        }

        return count;
    }
}

Related Tutorials