Android Tool class to see if services are open

Source: Internet
Author: User

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>
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.