简体   繁体   中英

Using Android app, service, sockets + google maps API - What is the right way?

My android app needs communicate with server when it's (application) opened. For that I want to use socketIO Java client.

Do I need to use BoundService for sockets (it should run on background)? Activity and Service communication needs to be possible. For that, I can use broadcast intents.

And where socket connection needs to established? In MainActivity onCreate or in service onStartCommand method?

EDIT:
Request from my server (sockets) comes before google maps gets ready.. This means, i can't add markers to map etc. What is the best solution? Emiting to server something like "Google maps ready", and then emit from server to android?

I think you should create the socket in the service, that way you can send messages from any activity / fragment easily. Just make sure to close the socket when apps goes to background.

You can rely on push notifications when the socket is not connected. As soon as user comes back to your application you can establish the socket again, this way you will save battery.

Make sure to use the partial wake locks in case you need to do some operation on the socket when the application is in background.

EDIT : If you want the messages to come only after map is ready then probably create the socket after map has been rendered. A possible alternative could be store the message from the server in a queue till it is processed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM