M
It all depends on the application you want to do, I will respond generically so that you apply the use on different occasions.It is recommended to delegate connection management (connect, disconnect) and sending messages to a separate class (by
example a Singleton)?See this: https://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons Some programmers consider Singleton an "anti-pattern", avoid using it, it can bring you barriers while developing the application. But yes, you must have a class specialized in receiving connections, another to create connections and one shared by the previous two to transmit your messages, reduce the scope to this at this stage to avoid any unnecessary coupling (Activties for example, these classes do not know that they exist).The use of broadcasts to pass a message received to the current Activity seems to be the appropriate choice in case of a
relatively simple application architecture. Does anyone disagree?
Would prefer an Event Bus or then coupling (bind) to a
service?The app is simple, so your code should be simple, but on occasions when multiple Activities can receive messages, notifications, alerts, etc. delegate all these tasks to a "Bound Service". http://developer.android.com/guide/components/bound-services.html Use the service to send and receive messages over the network and you can create notifications about new messages.The system is expected to kill the application process to release memory, but the connection must be reestablished as soon as there is
memory available for that. What's the best way to do that?
Scheduling periodic checks of the connection via AlarmManager does not seem to me
appropriate, and do not even use startForeground(), which in addition to not guarantee that
the process is killed (it only makes it less likely to happen)
still displays the user an uncomfortable notification that the application
is running, which even above if it is clicked leads to a screen
offering the option to terminate the application. I thought I'd come back
START_STICKY in the Service.startCommand() method could help in this
sense, but it doesn't seem to be done for that.generally keep this simple:When creating the service: Open connectionClose the service (it is not guaranteed to be called): Try to close connection.Service does not have a "onServiceRestoreState" method or something, so you can take advantage of the methods onDestroy, onUnbind, onTrimMemory, startForeGround, stopForeground to undo the connection. use this also: http://developer.android.com/reference/android/app/Service.html#START_REDELIVER_INTENT . This will cause onStartCommand to be called again and you can restore the connection.about the AlarmManager:
https://stackoverflow.com/questions/19411744/android-when-a-service-is-killed-how-can-we-persist-the-service-state-for-late The use of AlarmManager is assigned to tasks that must be performed from time to time, as stated in the above question,The intensive use of a service is hostile.Tip: You do not need to be listening for new messages or notifications from a server all the time (imagine the memory load of a server with multiple open connections, many perhaps without transmitting data for long periods). Make periodic checks (whicheverever 1 minute is so necessary, open connection to the server, check and close it).I'm not sure how the connection reestablishment should work in order to process the queue of messages to be sent. I suppose.
involves listening to the broadcast CONNECTIVITY_CHANGE or else an event
reestablished connection. How best to do this, ensuring
that the app tries to send messages when possible?It is trivial, but there are aspects that should be considered: Yes, check CONNECTIVITY_CHANGE to know whether or not you have connection available http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html Treat exceptions from Socket, Tcp, etc. (you may have network on the device, but the server may be offline or some network error occurs) as lost connection in your implementation. in performing any action: attempt to send message, read server notifications check this "unconnected state" and disconnected try reconnecting to send the request. When you are offline or receive an exception clean resources (Dispose Socket, Tcp, etc.), when trying to reconnect you will create them again.But in short: When carry out network operations you treats the connection state.After there is consensus regarding the modeling of the application, I think it will be necessary to include Wake Locks to the code to prevent the
device enter sleep mode while processing incoming messages or
placed in the shipping queue. But I'm not sure which part
the code includes them and would like recommendations. For that I have
also the following additional considerations: a) Once the connection
established, the device can enter sleep mode quietly that
the arrival of data will wake you up. But it is not guaranteed that these
data are processed in your completeness without a Wake Lock, before
device to sleep again; O http://developer.android.com/reference/android/os/PowerManager.WakeLock.html is designed to prevent the device from entering Idle, but this is not a viable means of waking it when receiving a notification/message.The most indicated in this case would be the http://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock.html which prevents the network device from being turned off when the mobile device enters Idle.you will use the method aquire to say that the device cannot turn off your network device and release after closing the connections, closing the service or closing the actions of the app.(b) If the app is asleep and
connection become available, a desirable requirement may be that the
queue of messages to send is processed in your completeness before the
device back to sleep. I'm not sure where to add the
Wake Lock in that case. for both WakeLock and WifiLock: wakeLock.acquire();
this.processQueue();
wakeLock.release();
(c) Even if the library works with
chosen communication protocol is prepared to work with
Android (i.e. manage input/output on one or more threads
secondary and not in the UI thread), it may be programmed
to run your callbacks (in particular the callback you receive
messages) on the thread of IU. This can affect the way I put it.
Wake Locks in my code.Yes, keep the WakeLocks within the classes that manage the messages (such as coupling, perhaps an IoC here) and try to keep the code "Thread-Safe", the rest is on account of the dependency injection.Is there a need to keep an active thread in continuous loop to keep the connection open? How can I avoid that?if you need the open connection all the time: Yes (not recommended) in some Thread she will need to be (this is inevitable), no need to be in loop, but the process may be in Idle, waiting for the messages.
Keeping an open connection for long periods is well prone to failures (mainly mobile networks), consider that the client questions the server about notifications over long periods rather than keeping the connection open and waiting for messages.