Android Java Service and Timer/Thread Sleep -


basically, need code simple, i'm beginner, still confused service, intentservice, creating new thread separate main activity/ui thread, etc. i'll explain task first , then, how think might best achieved.

task

the main activity/ui has 2 buttons, start , stop. app should make cycle of 2 tasks, taska , taskb. when start pressed, cycle executed. cycle has these steps inside-

  1. update status on main activity's ui: "taska execution in 60 seconds"
  2. wait 60 seconds
  3. update status ... : "taska executing"
  4. execute taska
  5. update status ... : "taskb execution in 120 second"
  6. wait 120 seconds
  7. update status ... : "taskb executing"
  8. execute taskb

on completion, cycle repeats. cycle must continue until user presses stop. cycle must not stop, interrupted or destroyed if main activity/ui onpause, onstop, or ondestroy. main activity/ui should separate cycle thread.

stop button: stops execution of steps inside cycle, regardless of step is. cancel scheduled tasks , stop service. state app should @ must if app being started first time (the same state before start pressed).

problem

i unsure full functions of service , intentservice. know service can perform multi-threading whereas intentservice performs task queue , performs them 1 one.

potential solution

my cycle above needs perform 8 steps in exact order intentservice ideal solution.

but question is: can create intentservice independent main activity/ui thread not rely on main activity's lifecycle or thread?

if not, ideal alternative achieve this?

thanks

my cycle above needs perform 8 steps in exact order intentservice ideal solution.

not really. intentservice designed transactional sorts of work: few seconds, or maybe minutes, of work, go away. need run more indefinitely.

can create intentservice independent main activity/ui thread not rely on main activity's lifecycle or thread?

an intentservice has little process' main application thread.

a better solution regular service. use standard java scheduledexecutorservice handle timing of events , triggering work on background thread. start work in onstartcommand() of service, triggered startservice() call activity. stop work in ondestroy() of service, triggered stopservice() call activity. in between startservice() , stopservice(), service run... while, @ least.

even better, if polling period longer, use alarmmanager , have give control @ event points in time. way, service not clogging memory of time. stands, users prone task-kill app, if not value service.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -