Using Android Apps, Services, Sockets + Google Maps API – What is the Right Way?
My android app needs to communicate with the server when (the app) opens.
To do this, I want to use the socketIO Java client.
Do I need to use BoundService for my socket (it should run in the background)? Activity and service communication needs to be possible. To do this, I can use the broadcast intent.
And where do you need to establish a socket connection? 在 MainActivity onCreate or service onStartCommand 方法中?
Requests to self’s servers (sockets) are made before Google Maps is ready. This means, I can’t add tags to map etc. What is the best solution? Send something like “Google map ready” to the server, and then send it from the server to Android?
I think you should create sockets in the service so you can easily send messages from any activity/fragment. Just make sure to close the socket when the app goes into the background.
You can rely on push notifications when the socket is not connected. Once the user returns to your application, you can establish the socket again, which saves battery power.
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.
If you want the message to appear only after the map is ready, you might create a socket after the map is rendered. One possible alternative is to store a message from the server in a queue until it is processed.