multithreading - Android Service vs Simple class with thread for network access -


i in quite dilemma how solve problem regarding network access.

all of rest based requests routed through own httprequestexecutor class execute each request asynchronously , results sent via handler passed per request.

some of these requests originating ui , needs end there example sign in request. in cases user has wait till finishes. write high level , specialized class each of such use case may business level processing before and/or after request executed. result handed down originating activity.

this done either using service or simple java class both using thread. not sure way go.

a simple java class straight forward , simple solution feel service may correct way it. concerned using service boiler plate code goes using i.e. binder or messaging communicate activity.

for problem correct way solve? service provide advantages in case?

in opinion, using java multi-threading mechanisms may better android services lightweight , short tasks.

as far know, google recommended use services long , continuous tasks. (e.g. file watchdogs, network-based updating agents)

i myself use following pattern multi-threading tasks.

// bind handler main thread. handler handler = new handler();  new thread(new runnable(){     @override     public void run(){         // task          // inform main thread, e.g. calling callbacks         handler.post(new runnable(){             @override             public void run(){                 // inform task requester here             }         });     } }).start(); 

also notes:

  • services run on main thread.
  • services not finish once task has been finished.

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -