Note: This article is changed from http://blog.csdn.net/android_tutor/article/details/5824581
In fact, the main use of Activitymanager getrunningservices to get a list of running service. It then uses the name of the running service to compare the alignment.
Look at the code:
Package Com.example.runningservice;import Java.util.list;import Android.app.activity;import Android.app.activitymanager;import Android.os.bundle;import Android.widget.textview;public class RunningService Extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); TextView Mtextview = new TextView (this); Activitymanager Mactivitymanager = (activitymanager) getsystemservice (Activity_service); list<activitymanager.runningserviceinfo> mservicelist = mactivitymanager.getrunningservices (+);//The Service name which u want to judgefinal String musicclassname = "Com.android.launcher2.MusicService"; Boolean B = Musicserviceisstart (mservicelist, musicclassname); Mtextview.settext ("The service U need to judge is:" +b+ "\ n" + "All are:" +getserviceclassname (mservicelist)); Setcontentview (Mtextview);} Judge the service is Startprivate boolean Musicserviceisstart (list<activitymanager.runningserviceinfo> Mservicelist,string className) {for (intI=0;i<mservicelist.size (); ++i) {if (Classname.equals (Mservicelist.get (i). Service.getclassname ())) {return true;}} return false;} Get all service name of Listprivate String Getserviceclassname (list<activitymanager.runningserviceinfo> mservicelist) {String res = ""; for (int i=0;i<mservicelist.size (); ++i) {res +=mservicelist.get (i). Service.getclassname () + "\ n";} return res;}}
Effect:
The path of the Android guru gets the running service and determines if a service is running