java - Best way to implement Socket.io in android -


i planning implement socket.io in android this library chat based application. far understood library seems pretty good. want know how maintain single socket connection throughout app time? here have listed out ways achieve, in need best , stable way.

three ways

mainapplication class extends application

by have scope socket connection maintained in main thread( or application's life cycle) , whenever socket instance needed activity can easily. it's main thread problem. might block main thread.

boundservice

by way can bind service activities , can use it. doing in separate thread way achieve io/network calls. cross processing transfer more expensive directly accessing in same process.

singleton

maintaining connection in singleton makes sense. don't know when instance killed process, because doesn't work in activity life cycle.

if makes sense please me out. if not comment out.

edit

i have given answer more suitable me.

first of all, application's oncreate irrelavant use case because can't have thread running in background when first initiated in non service code.

also, recommend using google cloud messaging instead of creating own mechanism. best device's battery life , less code handle.

if want implement chat on own, service choice. can combine singleton, wouldn't recommend approach. can use broadcasts , broadcast receivers communicating between service , activities, think easier boundservice since bounding service asynchronous , creates lot of mess compared simple broadcasting.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -