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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:27:36+00:00 2026-06-18T05:27:36+00:00

I have an android application that is connected to the computer via USB cable.

  • 0

I have an android application that is connected to the computer via USB cable. I use a TCPServer Class to send messages and listen. For example:

When I send a message like: request:x
I get the response: response:x:55

I need to make changes on my activity according to the response I get. At the moment I temporarily solved the problem by passing activity and activity class object to the TCPServer’s constructor

public TCPServer(int portNum, Activity activity, IntroActivity ia) {
    super();
    port = portNum;
    this.activity = activity;
    this.ia = ia;
}    

Then after I receive the response:

void updateButton(final int color, final String txt) {
    activity.runOnUiThread(new Runnable() {
         public void run() {
             ia.getConnectionButton().setBackgroundColor(color);
             ia.getConnectionButton().setText(txt);
        }
    });
}   

As you see, this is not effective at all. I need to somehow notify the activity whenever a relevant variable is received. I use a Class for GlobalVariables and change those static variables after listen(), however I am having troubles notifying the activity.

  • 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-18T05:27:37+00:00Added an answer on June 18, 2026 at 5:27 am

    First of all, it is almost always bad practice to pass Activity instances around. This is a time when it’s bad.

    Define an interface and use a callback to let the activity know that a response has been received.

    public interface ResponseReceivedListener {
        void onResponseReceived(int arg1, string arg2); // <- add arguments you want to pass back
    }
    

    In your TCPServer class:

    ArrayList<ResponseReceivedListener> listeners = new ArrayList<>();
    
    // ...
    
    public void setResponseReceivedListener(ResponseReceivedListener listener) {
        if (!listeners.contains(listener) {
            listeners.add(listener);
        }
    }
    
    public void removeResponseReceivedListener(ResponseReceivedListener listener) {
        if (listeners.contains(listener) {
            listeners.remove(listener);
        }
    }
    

    When you receive a response:

    for (ResponseReceivedListener listener : listeners) {
       listener.onResponseReceived(arg1, arg2);
    }
    

    In your Activity:

    public class MainActivity extends Activity implements ResponseReceivedListener {
    
        // ...
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            // ...
    
            tcpServer.setResponseReceivedListener(this);
    
            // ...
        }
    
        public void onResponseReceived(int arg1, string arg2) {
            // do whatever you need to do
        }
    
        // ...
    }
    

    All from memory so please excuse typos.

    This approach decouples the classes. The TCP Server has no knowledge of the activities. It simply calls back to any listeners registered. Those listeners might be Activities, they might be services. They might be instances of MySparklyUnicorn. The server neither knows nor cares. It simply says "if anyone’s interested, I’ve received a response and here are the details".

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

Sidebar

Related Questions

I have to do an Android application that reads value via Bluetooth from a
I have an Android application that connects to Facebook to request authorization of an
I have an android application that launches a child Activity under certain situations. Both
I have an Android application that uses the Android database to create tables and
I have an android application that displays a toast every hour and then every
I have an android application that requres a login based on users in a
I have an android application that has 3 activites. For the 1st and 2nd
I have created an android application that calls (using kSOAP library) a SOAP based
I have a simple Android application that uses a JAR I have built. When
I'm developing a simple Android application that would have an action bar on the

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.