Android advanced tutorial (18)-list all started services on Android devices and determine whether a service is enabled!

Source: Internet
Author: User

Hello everyone, I haven't seen you for a long time. I have previously made a music player to share with you a question during development, because I usually use services to play music.

When the user clicks the play button, the music is played in the service, and then the user exits the program (the service process is still in progress, and the music continues), so when the user clicks to enter the application again, our player must be in the playing status. Here I made a simple process to determine whether the music service is enabled.

The following is an example of how to list all started services on an Android device and determine whether a service is enabled:

Step 1: Create an android project named runningservice.

Step 2: Modify runningservice. Java code as follows:

Package COM. tutor. runningservice; <br/> Import Java. util. list; <br/> Import android. app. activity; <br/> Import android. app. activitymanager; <br/> Import android. OS. bundle; <br/> Import android. widget. textview; <br/> public class runningservice extends activity {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> // setcontentview (R. layout. main); <br/> textview mtextview = new textview (this); <br/> activitymanager mactivitymanager = <br/> (activitymanager) getsystemservice (activity_service ); </P> <p> List <activitymanager. runningserviceinfo> mservicelist = mactivitymanager. getrunningservices (30); <br/> // name of the service to be judged. I added a music service to launcher2 <br/> final string musicclassname = "com. android. launcher2.musicservice "; </P> <p> Boolean B = musicserviceisstart (mservicelist, musicclassname); </P> <p> mtextview. settext ("the service status you want to determine is:" + B + "/N" + getserviceclassname (mservicelist); <br/> setcontentview (mtextview ); <br/>}< br/> // determine whether to start a service using the service class name <br/> private Boolean musicserviceisstart (list <activitymanager. runningserviceinfo> mservicelist, string classname) {</P> <p> for (INT I = 0; I <mservicelist. size (); I ++) {<br/> If (classname. equals (mservicelist. get (I ). service. getclassname () {<br/> return true; <br/>}< br/> return false; <br/>}< br/> // obtain the class names of all started services <br/> private string getserviceclassname (list <activitymanager. runningserviceinfo> mservicelist) {<br/> string res = ""; <br/> for (INT I = 0; I <mservicelist. size (); I ++) {<br/> res + = mservicelist. get (I ). service. getclassname () + "/N"; <br/>}</P> <p> return res; <br/>}< br/>}

 

Step 3: run the above project to check the effect!

 

I don't know what it means. I was shocked at the first glance. Hey, with so many services, it's no wonder that my cell phone will soon be out of power.

 

 

Many of the above services are built-in and invisible to the system. You can view them at Settings> Applications> running services.

A total of 10 services are available.

 

Okay, so much today, huh, continue to work ..............

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.