This is also learned yesterday, do a summary.
Check whether the service is open to write a tool class, easy to use, the name of the service return Boolean value, of course, because of the need, but also to pass a contextual context.
Say a few key points about this tool class:
1. Methods to pass the context and Servicename,context used to getsystemservice () operation to obtain Activitymanager. Note that this method parameter is to be used in uppercase context parameters: Context.activity_service, otherwise it will be wrong, still do not know which mistake, it took me 10 minutes to know, to write down.
2.ActivityManager instances can be Getrunningservice () method, the parameter is to obtain the maximum number of services, generally 100 good.
3. The above method returns a list of things to be traversed by him, obtained by the name of each service, in the comparison, returns the result.
Here is the specific code.
1 2 3 4 5 6 7 8 9, |
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 (100) ; for (Runningserviceinfo info:infos) { &nBsp; string className = Info.service.getClassName (); if (Servicename.equals (className)) return true; } return false; &NBSP;&NBSP;&NBSP;&NBSP}}</runningserviceinfo> |