Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.app.ActivityManager;
import android.content.Context;

public class Main {
    /**
     * To check if the specified service is running.
     * This util needs `android.permission.GET_TASKS` permission.
     *
     * @param  context   the context
     * @param  className class name of service
     * @return           true if the service is running
     */
    public static boolean isServiceRunning(Context context, String className) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo info : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (info.service.getClassName().equals(className)) {
                return true;
            }
        }

        return false;
    }
}