java - After changing a variable, do I have to restart? -
i have long running service, consists of 2 threads.
//my service: if(userenabledprocessone) //shared preference value i'm getting settings of app based on checkbox processonethread.start(); if(userenabledprocesstwo) processtwothread.start();
basically, give user option enable/disable these processes checkbox. now, if user decides disable 1 of processes while service running, need restart service updated shared preferences? so?
//in settings activity of app public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if ( ischecked ) { // change shared preferences make process enabled stopservice(myservice.class) startservice(myservice.class) } if (!ischecked) // change shared preferences make process enabled stopservice(myservice.class) startservice(myservice.class) }
now i've changed shared preferences, need relaunch service consisting of 2 threads? need threads? efficient?
thanks much,
ruchir
hmm, doing leeak leak way much, service class runs on thread, if call stopself()
, lets run keep mainthread alive stop leaving 2 thread
s play around.
threads not depend on service, have volatile
boolean
in service class so
private volatile boolean killthreads = false;//guess in case //flag binded ischecked of checkbox or //interface method
when want restart toggle killthreads = true; , , in implementation of threads workloads, need if in loop need check killthreads flag if true death time if not continue, if not in loop, still need checking flag after every major code line
example
//in service class private volatile boolean killthreads = false; //we have jumped thread - creating new instance while(!killthreads){ //if means if flag false keep loop //hard code comes here } //or if not running loop //in run method can check flag for(int =0; < 100; i++){ if(killthreads){ break;//or return if } //rest of code } //after loop can check again if(killthreads){ return; }
just check flag after concrete code lines
now in activity
//in settings activity of app public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { // change shared preferences make process enabled //what here update killthreads ischecked //so find way bind bind activity service class //there tons of example on here
hope helps
Comments
Post a Comment