To determine whether the service is running Android

The method parameters were two
AS provides shortcuts to the full path name of the class class 1.className Service "class name package name +", the following
Here Insert Picture Description
2.context context object
Here Insert Picture Description
invokes this method returns true if it is running, If false is not running.
Method code is as follows:

 public static boolean isServiceRunning(String className, Context context) {
        //进程管理者
        ActivityManager manager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
        //获取进程中正在运行的服务集合
        List<ActivityManager.RunningServiceInfo> runningservice = manager.getRunningServices(1000);
        //遍历
        for (ActivityManager.RunningServiceInfo runningServiceInfo : runningservice) {
            //获取正在运行的服务的标识
            ComponentName service = runningServiceInfo.service;
            //获取正在运行的服务的全类型
            String name = service.getClassName();
            //判断
            if (name.equals(className)) {
                return true;
            }
        }
        return false;
    }
Published 77 original articles · won praise 411 · views 270 000 +

Guess you like

Origin blog.csdn.net/qq_42761395/article/details/100173841