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