Tool class to see if the service is turned on in Android

Source: Internet
Author: User

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

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.