This is also learned yesterday, to do a summary.
Check whether the service is turned on to write a tool class, easy to use, the name of the service returned a Boolean value, of course, because of the need, but also a contextual context.
Say a few key points of this tool class:
1. method to transmit context and servicename,context used to getsystemservice () operation to obtain Activitymanager. Note that this method parameter should be capitalized in the context of the parameters: Context.activity_service, or else will be wrong, still do not know what is wrong, it took me 10 minutes to know, I would like to write down.
2.ActivityManager instances can be Getrunningservice () method, the parameter is to obtain the maximum number of services, General 100 is good.
3. The above method returns a list of things to be traversed, to get the name of each service, in comparison, to return the result.
Here is the specific code.
Import Java.util.list;import Android.app.activitymanager;import android.app.ActivityManager.RunningServiceInfo; Import Android.app.service;import Android.content.context;public class Servicestateutiles {public static Boolean Isservicerunning (context context, String ServiceName) {//Get service method parameters must be in uppercase context!!! Activitymanager am = (activitymanager) context.getsystemservice (Context.activity_service); list<runningserviceinfo> infos = am.getrunningservices (+); for (Runningserviceinfo Info:infos) {String ClassName = Info.service.getClassName (); if (Servicename.equals (ClassName)) return true; return false;}}
Tool class to see if the service is turned on in Android