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 7506269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:12:36+00:00 2026-05-29T22:12:36+00:00

I am trying to create an android app that communicates with a local server

  • 0

I am trying to create an android app that communicates with a local server through a socket. The communication passes simple commands and data in strings using JSON. The client should constantly listen to incoming messages from the server and update the user interface when new data is received.

So i have created a network service which is a bound service running in the background. From my activity i bind to the service and receives an instance of the service object. The service object contains instance methods which allow me to send commands to the server.

My problem is, how do i enable my service to constantly listen for messages from the server without blocking the possibility to send messages to the server?

private Socket socket = null;
private PrintWriter out = null;
private BufferedReader in = null;

private Listener listener;

private String host = "10.0.1.4";
private int port = 3000;

public NetworkService() 
{
    try {

        if (socket == null)
        {
        socket = new Socket(this.host, this.port);
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        }

        if(listener == null)
        {
            listener = new Listener();
            Thread thread = new Thread(listener);
            thread.start();
        }

        } catch (Exception e) {
            // ...
        }
    }
}

public class Listener implements Runnable
{

    @Override
    public void run() {

        try {
            String line = null;

            while((line = in.readLine()) != null)
                {
                // Do something. Never gets here

                }
            } catch (Exception e) {
                // ...
            }

        }

    }
  • 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-05-29T22:12:37+00:00Added an answer on May 29, 2026 at 10:12 pm

    You can create in your Service one thread for listening to the server. The second thread is for sending commands. Then for your service you should create a main thread with handler in it. This handler will process messages from this two threads.

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

Sidebar

Related Questions

I have an Android app that communicates with a server via asynchronous socket connection
I am trying to create a simple test app that basically extends the Android
I'm trying to create an Android client for a client/server app that has previously
I'm trying to create an app for Android that simply sends a command to
I am trying to create a simple Android application that has a ActivityList of
I'm trying to create an Android app that adds a random quote to images.
Hi im trying to create a simple android app using the Eclipse IDE, all
I'm trying to create an app that allows the user to open the android
I'm trying to create a ProgressDialog for an Android-App (just a simple one showing
I'm trying to create an Android app that searches for a certain type of

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.