How to use service in android? -




i making simple music app.
problem facing right how check id service running or not?

last time when made music app without service fine there problem in it. whenever played music pressed , exited app, after again opening app when again played music started song playing first song still playing whenever closed , opened app , played music started playing song other songs playing @ same time.

so using service wishing remove using bound service. want know when open , close app app remove instance of service self , if yes how can know service running , not bind again?

use following inside activity:

private boolean ismyservicerunning(class<?> serviceclass) {     activitymanager manager = (activitymanager)  getsystemservice(context.activity_service);     (runningserviceinfo service :  manager.getrunningservices(integer.max_value)) {         if (serviceclass.getname().equals(service.service.getclassname())) {             return true;         }     }     return false; } 

and call using:

ismyservicerunning(myservice.class) 

this works reliably, because based on information running services provided android operating system through activitymanager#getrunningservices.

all approaches using ondestroy or onsometing events or binders or static variables not work reliably because developer never know, when android decides kill process or of mentioned callbacks called or not. please note "killable" column in lifecycle events table in android documentation.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -