获取一个服务的开启状态

来源:互联网 发布:廉租住房软件 编辑:程序博客网 时间:2024/05/16 18:58

  • 前言
  • java代码

1.前言

安卓开发中,service是我们常用的组件之一,那么我们就很有必要判断当前service是否已经处于开启状态。

2.java代码

class ServiceUtils{    public static boolean isServiceRunning(Context context,String serviceFullName){        ActivityManager am =(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);        List<ActivityManager.RunningServiceInfo> runningServices = am.getRunningServices(200);        for(RunningServiceInfo service:runningServices ){            if(serviceFullName.equals(service.service.getClassName())){                return true;            }        }        return false;    }}
0 0