Judge whether the service is open - Android Android OS

Android examples for Android OS:Service

Description

Judge whether the service is open

Demo Code


//package com.java2s;

import java.util.List;

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

public class Main {

    public static boolean isServiceWork(Context context, String serviceName) {

        boolean isRunning = false;
        ActivityManager manager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

        List<ActivityManager.RunningServiceInfo> serviceInfos = manager
                .getRunningServices(100); /* There are services up to 100 */
        if (!(serviceInfos.size() > 0)) {
            return false;
        } else {/*from  ww  w  . j  av a 2 s  .c  o m*/
            for (int i = 0; i < serviceInfos.size(); i++) {
                if (serviceInfos.get(i).service.getClassName().equals(
                        serviceName)) {
                    isRunning = true;
                    break;
                }
            }
        }
        return isRunning;
    }
}

Related Tutorials