Android——显示当前运行所有服务,判断服务是否运行

1.显示系统当前所有运行服务:

ActivityManager am = (ActivityManager) MainActivity.this.getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : am.getRunningServices(Integer.MAX_VALUE)) {
            System.out.println(service.service.getPackageName()+"-----"+service.service.getClassName());
        }

2.判断某一个服务是否运行:

public static boolean checkServiceStatus(Context context) {
        boolean isServiceRunning = false;
        ActivityManager am = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : am
                .getRunningServices(Integer.MAX_VALUE)) {
            if (BackgroundService.class.getName().equals(service.service.getClassName())) {
                isServiceRunning = true;
                break;
            }
        }
        return isServiceRunning;
    }

 

 

posted @ 2013-08-07 16:07  似水流云  阅读(489)  评论(0编辑  收藏  举报