service - Handler does not work reliably in Android -


i have programmed app uses handler. inside handler there network operations. handler has interval of min*1000*60 ms. used handler min=5, should repeat after 5 minutes. result of check:

first handler:

16:20:22 16:25:23 17:01:52 17:13:07 17:20:19 17:25:55 

second handler:

16:20:26 16:25:26 17:01:35 17:12:51 17:20:02 17:25:37 

third handler:

16:24:58 16:31:59 17:12:43 17:19:54 17:25:30 

all handlers running in separate services. screen turned off. have ideas or alternatives handler in android?

the code of handlers simple:

handler.postdelayed(new runnable() {             public void run() {             // network operations             }         }, interval); 

first, assuming created handler via new handler(), work on main application thread. do not network i/o on main application thread. source of drift.

second, not need 3 services. usually, need 1 service. 3 services makes app more complicated no added value user.

third, handler work while device awake, , not know whether acceptable limitation or not.

fourth, using handler implies service(s) running indefinitely, , users not this. only have services in memory when actively delivering value user.

a better way implement this, therefore, use alarmmanager scheduled events. if not need events processed while device asleep, alarmmanager can directly pass control service. ideally, intentservice, service give background thread automatically , service automatically shut down when work complete.

if need events processed while device asleep, please give user control on event period, including option of "do not anything", waking device every 5 minutes network i/o bad battery. then, use a wakefulbroadcastreceiver or a wakefulintentservice arrange have work done while keeping device awake.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

mysql - Pass value between different pages (form) with PHP -