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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:13:38+00:00 2026-06-05T03:13:38+00:00

This is a common question, and I have read up on the various ways

  • 0

This is a common question, and I have read up on the various ways of handling it, but each on seems to fall short for what I am trying to do, which is essentially be a good OO-Citizen.

I have an Activity that invokes a CommunicationManager, which basically polls a TCP socket for data. When the CommunicationManager receives data, it throws a custom event (containing the string it just fetched), which is handled by the Activity. I am doing this, A) because other classes will depend on that data, not just the Activity, and B) because the polling is asynchronous, and should fire an event when it receives results.

My problem lies in that I need to surface those results into a TextView on the UI. I have the polling mechanism all set up, it fires every 1000ms, and invokes the event handler on the Activity. However, the UI never updates.

Assumedly this is a thread issue and the UI thread is not the one getting the change to the TextView, but how do I do this?? I have tried using a Handler, but am not sure where to put it, and when I did get it compiling it never updated the UI.

This seems relatively trivial if everything was done within the Activity, but adding in this other class (CommunicationManager) and the event is making it very confusing for me.

Here is what I have so far:

ACTIVITY (polling is invoked by clicking a button on the UI):

public void onClick(View v) {
    if (v.getId() == R.id.testUDPBtn) {
        statusText.setText("");

        commMgr = new CommunicationManager();
        commMgr.addEventListener(this);

        MediaPositionPollThread poller = new MediaPositionPollThread(commMgr);
        poller.startPolling();
    }
}

@Override
public void handleMediaPositionFoundEvent(MediaPositionFoundEvent e) {
    statusText.append(e.userData);
}

THREAD:

class MediaPositionPollThread extends Thread {
    private CommunicationManager commManager;
    private static final String TAG = "MediaPositionPollThread";
    private boolean isPolling = false;

    public MediaPositionPollThread(CommunicationManager cm) {
        commManager = cm;
    }

    public void startPolling() {
        isPolling = true;
        this.run();
    }

    public void stopPolling() {
        isPolling = false;
    }

    @Override
    public void run() {
        while (isPolling) {
            try {
                commManager.getCurrentMediaPosition();
                Thread.sleep(1000);
            }
            catch (InterruptedException e) {
                Log.d(TAG, "EXCEPTION: " + e.getMessage());
            }
        }
    }
}

COMMUNUCATION MANAGER:

public void getCurrentMediaPosition() {
    PrintWriter outStream;
    BufferedReader inStream;
    String resultString = "";

    try {
        outStream = new PrintWriter(tcpSocket.getOutputStream(), true);
        outStream.println("GET?current_pts");

        inStream = new BufferedReader(new InputStreamReader(tcpSocket.getInputStream()));
        resultString = inStream.readLine();

        fireEventWithData(resultString);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public synchronized void addEventListener(MediaPositionFoundEventListener listener)  {
    _listeners.add(listener);
}

public synchronized void removeEventListener(MediaPositionFoundEventListener listener)   {
    _listeners.remove(listener);
}

private synchronized void fireEventWithData(String outputString) {
    MediaPositionFoundEvent evt = new MediaPositionFoundEvent(this);
    evt.userData = outputString;

    Iterator<MediaPositionFoundEventListener> i = _listeners.iterator();
    while(i.hasNext()) {
        ((MediaPositionFoundEventListener) i.next()).handleMediaPositionFoundEvent(evt);
    }
}

So I have the Activity making a thread that gets executed every second, calling CommunicationManager >> getCurrentMediaPosition, which in turn fires the MediaPositionFoundEvent, which is handled by the Activity and updates the TextView (statusText) on the screen.

Everything works except the screen not updating. I have tried runOnUiThread, and a Handler, but am obviously not getting it right.

Thanks in advance for any insight or solutions!

  • 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-05T03:13:39+00:00Added an answer on June 5, 2026 at 3:13 am

    It looks like your blocking the UI thread with your custom Thread. Please update this method to call start() vs run().

    public void startPolling() {
        isPolling = true;
        this.start();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems this is a common question/problem but despite checking out a number of
I know this might be a common question but I have tried to search
This is a common question, but the explanations found so far and observed behaviour
I've read many different articles/threads on this, but have yet to determine the best
First, I know this is kind of common question, but I could not find
This may seem a common question but I googled to find the right answer
So, this is a common interview question. There's already a topic up, which I
This question crossed my mind after I read this post: Common REST Mistakes: Sessions
This has to be a common question that all programmers have from time to
This seemed like a common question but after doing some searching, I wasn't really

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.