I am studying Android and wrote a demo. I need to start the service in an activity. I feel that I should judge whether the service is running before it is started. Baidu has the following code to stay for backup.
<PRE name = "code" class = "Java"> // This method is used to determine your own service --> COM. android. controladdfunctions. whether phoneservice has run public static Boolean isworked () {activitymanager mymanager = (activitymanager) context. getsystemservice (context. activity_service); arraylist <runningserviceinfo> runningservice = (arraylist <runningserviceinfo>) mymanager. getrunningservices (30); For (INT I = 0; I <runningservice. size (); I ++) {If (runningservice. get (I ). service. getclassname (). tostring (). equals ("com. android. controladdfunctions. phoneservice ") {return true ;}} return false ;}
Source code
Determine whether the service is running in the activity