package com.aoleyun.sn.utils; import android.app.ActivityManager; import android.content.Context; import android.util.Log; public class ServiceAliveUtils { private static final String TAG = ServiceAliveUtils.class.getSimpleName(); public static boolean isServiceAlive(Context mContext) { boolean isServiceRunning = false; ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); if (manager == null) { return false; } for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (mContext.getClass().getName().equals(service.service.getClassName())) { isServiceRunning = true; } } Log.i(TAG, mContext.getClass().getName() + " :isServiceAlive: " + isServiceRunning); return isServiceRunning; } public static boolean isServiceAlive(Context mContext, String serviceName) { boolean isServiceRunning = false; ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); if (manager == null) { return false; } for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (serviceName.equals(service.service.getClassName())) { isServiceRunning = true; } } Log.i(TAG, serviceName + " :isServiceAlive: " + isServiceRunning); return isServiceRunning; } }