判断一个服务是否开启

判断一个服务是否处于运行状态, 很好用的方法, 需要知道Service的完整名称.
 1     public static boolean isServiceRunning(Context context, String serviceName) {
 2         // 得到一个应用管理器, 如同windows中的任务管理器一样
 3         ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
 4         List<RunningServiceInfo> services = am.getRunningServices(100);
 5         for (RunningServiceInfo sInfo : services) {
 6             String runningServiceName = sInfo.service.getClassName();
 7             if(serviceName.equals(runningServiceName)){
 8                 return true;
 9             }
10         }
11         return false;
12     }

 



posted @ 2014-07-09 11:40  高原4122  阅读(352)  评论(0编辑  收藏  举报