android - How to force an IntentService to stop immediately with a cancel button from an Activity? -


i have intentservice started activity , able stop service activity "cancel" button in activity. "cancel" button pressed, want service stop executing lines of code.

i've found number of questions similar (i.e. here, here, here, here), no answers. activity.stopservice() , service.stopself() execute service.ondestroy() method let code in onhandleintent() finish way through before destroying service.

since there apparently no guaranteed way terminate service's thread immediately, recommended solution can find (here) have boolean member variable in service can switched in ondestroy() method, , have every line of code in onhandleintent() wrapped in own "if" clause looking @ variable. that's awful way write code.

does know of better way in intentservice?

stopping thread or process dirty thing. however, should fine if service stateless.

declare service separate process in manifest:

<service      android:process=":service"      ... 

and when want stop execution, kill process:

activitymanager = (activitymanager) getsystemservice(activity_service); list<runningappprocessinfo> runningappprocesses = am.getrunningappprocesses();  iterator<runningappprocessinfo> iter = runningappprocesses.iterator();  while(iter.hasnext()){     runningappprocessinfo next = iter.next();      string pricessname = getpackagename() + ":service";      if(next.processname.equals(pricessname)){         process.killprocess(next.pid);         break;     } } 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -