java - Determine if an instance of a service is running and kill it -


i have android service, let's "service.java"

i start instance , pass unique id each instance of service start (so don't have 2 service same uid running @ same time):

intent intent = new intent(this, service.class); intent.putextra("key", uid); startservice(intent); 

say set variable in "service.java" uid:

private string uid; @override public int onstartcommand(intent intent, int flags, final int startid) {     super.onstartcommand(intent, flags, startid);      if(intent != null) {         bundle infobundle = intent.getextras();         this.uid = infobundle.getstring("key");     }      ... }  public string getuid() { return this.uid; } 

is there way somehow check if instance of "service.java" running given uid , kill it?

i have list of key/value pairs want start instances of service @ specific times of day, if user "disables" time slot (key/value pair), want service stop.

backstory:

i have application starts service 30 minutes before given time poll data source changes. service kills after data feed no longer available.

if user "unfavorites/disables" time slot (key/value pair - uid), don't want service running next 30 minutes, kill it's "unfavorited/disabled"

i may going wrong way.

either way, input appreciated, thanks!

i'd @ more as: service needs know when data model changes. in case, data model "list of key/value pairs", each enabled/disabled status.

assuming being stored in database, sharedpreferences, or file, singleton cache rapid access, here 3 choices letting service know data model change:

  1. use event bus or other listener pattern, if service around, finds out change , can take appropriate steps. if service running in separate process, event bus have system-level broadcasts (in contrast localbroadcastmanager, in-process event bus).

  2. front data model contentprovider , use contentobserver notified changes (which swatting fly buick, but, hey, it's option).

  3. call startservice() when change data model, information (e.g., custom action string) let service know "check data model" event. downside of approach start service if not started, though presumably service realize if there nothing , stop right away.

personally, i'd go option #1.


but, literal question, way pull data out of service on demand use binding pattern, client connects service via bindservice() , service publishes api in form of binder. not mutually exclusive current approach. however, downside of approach akin #3 above: if service not running, act of trying bind create instance of service.

there's nothing stopping taking "oh, service singleton, i'll track state via static field somewhere" approach. i've done on occasion scrap apps, though makes me queasy. works services in same process rest of app; if service in separate process, approach doesn't work.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -