28
First of all I apologise for the long text, and for the various points raised. I thought it best to cast everyone on a question just because they are interrelated and because I think anyone with experience in the type of app described (an Android client that maintains a continuous socket connection by receiving and sending data) is able to give your opinion on all or most of these points. Basically I need to make design decisions and I have doubts about what would be the "recommended Android way" to do.
I am trying to define what would be the minimum skeleton necessary to have a dsse type application, that is, to connect to a server via socket (Websocket, Socket.IO, TCP, etc.) and can exchange messages with him). I don’t have a specific use example; chat is not one of these examples, because in this case the ideal would be to use push notification.
This connection shall not be linked to Activities
of the application, that is, messages can be received even after the user has left the application.
Questions I’m trying to answer:
It is recommended to delegate connection management (connect, disconnect) and sending messages to a separate class (for example a Singleton)?
The use of Broadcasts to pass a received message to the
Activity
current seems to be the appropriate choice in the case of a relatively simple application architecture. Does anyone disagree? Would a Event Bus or coupling (bind) at a service?The system is expected to kill the application process to free up memory, but the connection should be reestablished as soon as there is memory available for it. What’s the best way to do this? Schedule periodic connection checks via
AlarmManager
I don’t think it’s appropriatestartForeground()
, that in addition to not ensuring that the process is killed (it just makes it less likely to happen) still displays to the user an uncomfortable notification that the application is running, which on top of that if it is clicked leads to a screen offering the option to terminate the application. I thought I’d returnSTART_STICKY
in the methodService.startCommand()
could help in that direction, but he doesn’t seem to be made for it.I’m not sure how connection reestablishment should work in order to process the queue of messages to send. I suppose it involves listening to the broadcast
CONNECTIVITY_CHANGE
or a reestablished connection event. What is the best way to do this, ensuring that the app tries to send messages when possible?After there is consensus regarding the modeling of the application, I believe it will be necessary to include Wake Locks the code to prevent the appliance from entering Sleep mode while processing incoming or queued messages. But I’m not sure which part of the code to include them in and I would like recommendations. For this I also have the following additional considerations:
a) Once the connection is established, the device can enter Sleep mode calmly that the arrival of data will awaken you. However it is not guaranteed that this data will be processed in its completeness without a Wake Lock, before the device goes back to sleep;
b) If the application is dormant and the connection becomes available, a desirable requirement may be that the message queue to be sent be processed in its entirety before the device goes back to sleep. I’m not sure where to add Wake Lock in that case.
c) Even if the library that works with the chosen communication protocol is prepared to work with Android (that is, manage the input/output in one or more threads secondary and not in the thread of UI), may be that it is programmed to execute your callbacks (in particular the callback receiving messages) on thread of UI. This can affect the way I dispose Wake Locks in my code.
There is a need to maintain a thread Continuous loop active to keep the connection open? How can I avoid this?
I appreciate any feedback. I also put this question on ONLY in English.
Piovezan, did you ever consider GCM as a way to implement Push Notifications? I have a suggestion to make, but it involves the same, when I have time I put an answer.
– Wakim
So, the idea in this case is not to use push notification; I have as an example an application that keeps the connection open only while the user is "logged in" on it for example, and lose the connection when dropping. There is no obligation for messages to be delivered to the customer if the connection is closed. They are short-lived and expendable data, for example in an application that runs a central alarm installed in a residence.
– Piovezan
– epx