Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8565491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:26:20+00:00 2026-06-11T17:26:20+00:00

I have built an Android chat application. It has a TCP server which is

  • 0

I have built an Android chat application. It has a TCP server which is connected to via using a Socket.

I want the server connection to work in the background always – even if the app is not in foreground – and listen to data sent from the server.

For now I am using a while loop to checking if there is something new, but the OS close my thread when Android needs resources.

I have thought about using the readline() method in my thread instead of using the while loop because readline() is a blocking method.

Is this the right approach to take, which will lead to, prevention of the OS from killing my application?

Is there any way, like a custom broadcast receiver that will launch only when the incoming socket is available?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:26:21+00:00Added an answer on June 11, 2026 at 5:26 pm

    In short, Android can and will kill any applications that hog up resources in order to keep running. The onus is on you on how to handle the scenarios which can threaten your app to be killed.

    I suggest looking at the service’s life-cycle as found on the developer‘s site.

    For a start, any application, be it service/activity, that hogs up too much, in this manner is considered… “rude” in the eyes of Android, and therefore, is prepared to be killed in this manner regardless!

    For example, listen in the onLowMemory override method and act accordingly, such as saving data and what-nots.

    What really, should be happening, is this, the service, spawns a thread to periodically listen for incoming connections in this manner,

    while (!terminating){
         Socket incomingClientSocket = ourServerSocket.accept();
         // How you handle this part is up to you?
         Thread.sleep(500); // Sleep for 500 ms
    }
    

    The terminating variable is a boolean and is the deciding variable that controls when to terminate the loop. Notice how the Thread.sleep method is used, to “calm down” the execution, if omitted, Android will put a cross-hair on the app and terminate it, in other words, be polite to system resources.

    Hint: Extend the Thread class, to hold the functionality of handling incoming connections via the accept() method.

    When the incomingClientSocket becomes non-null, then another thread is created in which it opens the input/output stream of the incomingClientSocket and read/write to it using binary fashion.

    Do not use the readline method as that is an indication of poor design and assuming the data is text-ual, because one incoming packet to the client could be for example, 720bytes, the next packet coming in after that, could well be 564 bytes, that is the nature of TCP/IP.

    You need to come up with a more reliable way of establishing boundaries for the data transmission, for example, a begin and end marker, and let the server read the incoming data and distinguish the byte stream that composes of a begin and end markers, once both are present, then extract the real data in-between the markers and act on it accordingly.

    You could for instance, say, upon incomingClientSocket that actually becomes non-null, send a broadcast to your activity to act on it, and let the activity, take that socket, and open the connection and read/write to/from the input/output streams associated with the socket.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built an android application which has a single mapview. I have a
i have an android project which has a lot of classes I've built. i
I have built an Android application using PhoneGap and for tweeting I have used
I have built an android app which use Google Map feature. I want my
I have a simple Android application that uses a JAR I have built. When
I have Android pet-project DroidIn which utilizes HttpClient 4 (built into Android) to do
I have build my very first application to Android and I want now to
I want to build an notepad-style application on android that will have syntax highlighting.
I have built ffmpeg for Android using the code and method described at https://github.com/halfninja/android-ffmpeg-x264
I have built an activity to handle gestures in my Android game and want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.