The path of the Android guru gets the running service and determines if a service is running

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.