continuation of Baidu map positioning of the demo. The use of service to Baidu positioning, and upload data to the server encountered a problem: in the real machine after the use of clean memory to close the program, the service will be shut down, but in a few seconds, it will actively restart itself. Restart even if, and re-login to the system, it will open a similar service, in the Logcat will see every time to get to two times the location data.
Then think about whether the service was created before it was established? Just be able to make this inference. Then the service exists for us, regardless of it, if it does not exist then create. In this way, Baidu found it feasible (servicebackground services are best to infer whether the existence of), code such as the following:
Private Boolean isworked (String className) {Activitymanager Mymanager = (Activitymanager) LoginActivity.this.getApplicationContext (). Getsystemservice (Context.activity_service); Arraylist<runningserviceinfo> Runningservice = (arraylist<runningserviceinfo>) Mymanager.getrunningservices (int.); for (int i = 0; i < runningservice.size (); i++) {if (Runningservice.get (i). Service.getclassname (). toString (). Equals (ClassName)) {return true;}} return false;}
Inference process:
if (!this.isworked ("package. Service Name")) {Intent Intent = new Intent (); Intent.setaction ("Action for intent-filter of the serviced Component");// Start Servicestartservice (intent);} ELSE{LOG.I ("info", "Service has been started!")!
");}
android--Infer if service has started