Checks to see if a Form is running. - Android Network

Android examples for Network:Network Status

Description

Checks to see if a Form is running.

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 {
    /**//w ww . j  ava 2  s .  c  o m
     * Checks to see if a Form is running. This only checks to see if the Form is the "top Activity".
     * @param context
     * @param className The name of the Form to check
     * @return
     */
    public static boolean isFormRunning(Context context, String className) {
        ActivityManager manager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        StringBuilder b = new StringBuilder();
        b.append("ComponentInfo{");
        b.append(context.getPackageName());
        b.append("/");
        b.append(context.getPackageName());
        b.append(".");
        b.append(className);
        b.append("}");
        String actName = b.toString();
        List<RunningTaskInfo> activities = manager
                .getRunningTasks(Integer.MAX_VALUE);
        int size = activities.size();
        for (int i = 0; i < size; i++) {
            if (activities.get(i).topActivity.toString().equalsIgnoreCase(
                    actName)) {
                return true;
            }
        }
        return false;
    }
}

Related Tutorials