Ok so to explain I have server in Java that receives data from a C++ program that I have no control over. So the data is transferred over TCP/IP and the Java server just receives it. The volume of incoming data is huge and it comes at a very fast rate, just imagine a terminal printing loads of lines really fast.. I don't need to get all of it, just as much as I can would be great..

Now I need to send that data to an android device and display it. I have no idea what is the best approach to so this.

I am currently pinging the server from my android device every 50ms using an executor. Then I use a handler to display the data. I have already posted a question with code here: related question.

Obviously the android app will need to ping it while the app is running so I can't avoid the while(true) scenario.

I have read about 100000000 ways to do concurrency in Android, and I do not know what to choose for this.

  • I tried simple Threads, they were to hard to manage.
  • I am now running a ExecutorService
  • I also read about ScheduledExecutorService, but they need a time duration right ? My app can be running for 5 hours or 5 minutes or 5 years.
  • I read about HandlerThreads too, but I am not sure they are ideal for this scenario.

Any help would be great.

put on hold as too broad by Robert Harvey Aug 22 at 14:43

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Since I don't do Android development, I can't answer the specific question, but I can say polling every 50ms is probably too often. You don't need to poll more often than you update the display. Most users won't notice a change shorter than 100ms, and many times you only need to update once a second or so. That can save some battery life for your users (as well as sleeping for a bit until you need to do more work) – Berin Loritsch Jun 27 '18 at 14:54

I cannot say best but from my POV you need to use Push Notification. So logic will be you register the device with the server when the user logs in and unregister when user logouts. Now when you have new data on the Server end, the server will send a notification to the device and after receiving notification, from android device you can send the request to server for downloading the data. On the server end, you can implement the logic that there should be at least x ms or sec time difference between two notification. This will help you to avoid sending request too frequently and improve battery saving. Sending request to server every 50 ms will surely drain your device battery. In the approach i mentioned, you only need to download data when new data is available; so in any case, your server did not receive new data after your last download, you will not require to send download request from the device.

For Push Notification, you can use FCM.

Not the answer you're looking for? Browse other questions tagged or ask your own question.