networking - How do I organize this Android app with network i/o and multiple activities? -


to begin with, first android app i'm writing, , have little prior java experience. nothing simple -- missing "obvious".

here's general design i'm dealing with:

  • a long-lived bidirectional network connection.
  • requests should go out on network when user interacts ui.
  • the ui should updated when responses said requests come -- asynchronously.
  • the app contain multiple activities.

these activities focused on particular areas of functionality available, relying upon same underlying network connection. want set connection no matter activity app starts in, , have survive across switching activity in app, want shut down when switching away app completely.

i think want threads. i've got basic working, not because don't think have them organized properly. am, far, unable pass data between ui , network thread, can't requests in nor actions responses out. i'd appreciate advice.

i think want threads.

you don't have choice on front. need thread listens on socket incoming data. android no different standard java in respect.

i am, far, unable pass data between ui , network thread, can't requests in nor actions responses out.

well, thread should managed service. network connection supports multiple activities, no 1 activity should own thread.

you need decide when network connection should exist. since activities come , go, need decide if network connection should exist when 1 of activities in foreground (in case bind service bindservice() each activity), or whether there explicit "start" , "stop" operation user must do, connection can live after of activities gone (in case use startservice() instead of bindservice()).

once know when , how starting/stopping service, can decide how service communicate results various activities. there tons of options, better others depending on use case. registered listeners, messenger, broadcast intents, contentprovider, , on candidates. of can used background thread , can arrange data foreground activity on main application thread. other activities typically refresh data during onresume(), since there no point in proactively updating them when not on screen or may have been kicked out of ram.

iow, "advice" several chapters in book or two. :-)


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -