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

  • Home
  • SEARCH
  • 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 689957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:23:10+00:00 2026-05-14T02:23:10+00:00

Im working on a small application to try out an idea that I have.

  • 0

Im working on a small application to try out an idea that I have. The idea is to periodically update the UI when event of some sort occurs. In the demo I’ve created, I’m updating a ProgressDialog every 2 seconds for 15 turns.

The problem I am having, which I don’t quite understand is that when an event is handled, I send a message to the handler which is supposed to update the message in the ProgressDialog. When this happens however, I get an exception which states that I can’t update the UI from that thread.

The following code appears in my Activity:

ProgressDialog diag;
String diagMessage = "Started loading...";

final static int MESSAGE_DATA_RECEIVED = 0;
final static int MESSAGE_RECEIVE_COMPLETED = 1;


final Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg){
        diag.setMessage(diagMessage);

        switch(msg.what){
            case MESSAGE_DATA_RECEIVED:

                break;
            case MESSAGE_RECEIVE_COMPLETED:
                dismissDialog();
                killDialog();
                break;
        }
    }
};

Boolean isRunning = false;

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setupDialog();
    if(isRunning){
        showDialog();
    }

    setContentView(R.layout.main);


}

void setupDialog(){
    if(diag == null){
        diag = new ProgressDialog(ThreadLoading.this);
        diag.setMessage(diagMessage);
    }
}

void showDialog(){
    isRunning = true;
    if(diag != null && !diag.isShowing()){
        diag.show();
    }
}

void dismissDialog(){
    if(diag != null && diag.isShowing()){
        diag.dismiss();
    }
}

void killDialog(){
    isRunning = false;
}

public void onStart(){
    super.onStart();

    showDialog();



    Thread background = new Thread(new Runnable(){
        public void run(){
            try{
                final ThreadRunner tr = new ThreadRunner();
                tr.setOnDataReceivedListener(new ThreadRunner.OnDataReceivedListener(){
                    public void onDataReceived(String message){
                        diagMessage = message;
                        handler.handleMessage(handler.obtainMessage(MESSAGE_DATA_RECEIVED));
                    }
                });

                tr.setOnDataDownloadCompletedEventListener(new ThreadRunner.OnDataDownloadCompletedListener(){
                    public void onDataDownloadCompleted(String message){
                        diagMessage = message;
                        handler.handleMessage(handler.obtainMessage(MESSAGE_RECEIVE_COMPLETED));
                    }
                });

                tr.runProcess();
            }
            catch(Throwable t){
                throw new RuntimeException(t);
            }
        }
    });

    background.start();
}

@Override
public void onPause(){
    super.onPause();
    dismissDialog();
}

For curiosity sake, here’s the code for the ThreadRunner class:

public interface OnDataReceivedListener {
    public void onDataReceived(String message);
}

public interface OnDataDownloadCompletedListener {
    public void onDataDownloadCompleted(String message);
}

private OnDataReceivedListener onDataReceivedEventListener;
private OnDataDownloadCompletedListener onDataDownloadCompletedEventListener;


int maxLoop = 15;
int loopCount = 0;
int sleepTime = 2000;

public void setOnDataReceivedListener(OnDataReceivedListener onDataReceivedListener){
    this.onDataReceivedEventListener = onDataReceivedListener;
}

public void setOnDataDownloadCompletedEventListener(OnDataDownloadCompletedListener onDataDownloadCompletedListener){
    this.onDataDownloadCompletedEventListener = onDataDownloadCompletedListener;
}

public void runProcess(){
    for(loopCount = 0; loopCount < maxLoop; loopCount++){
        try{
            Thread.sleep(sleepTime);
            onDataReceivedEventListener.onDataReceived(Integer.toString(loopCount));
        }
        catch(Throwable t){
            throw new RuntimeException(t);
        }
    }

    onDataDownloadCompletedEventListener.onDataDownloadCompleted("Download is completed");
}

Am I missing something? The logic makes sense to me and it looks like everything should work, I’m using a handler to update the UI like it is recommended.

Any help will be appreciated.

Thanks,
Tyrone

P.S. I’m developing for Android 1.5

  • 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-14T02:23:10+00:00Added an answer on May 14, 2026 at 2:23 am

    I found the problem. After comparing my code with someone else’s code which was very similar, the following small problem was found:

    handler.handleMessage(handler.obtainMessage(MESSAGE_RECEIVE_COMPLETED));

    Should actually be:

    handler.sendMessage(handler.obtainMessage(MESSAGE_RECEIVE_COMPLETED));

    Hopefully someone finds this useful and learns from my mistake 🙂

    Regards,
    Tyrone

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

Sidebar

Related Questions

I have a small application I am working on that at one point needs
I am working on a small AIR desktop application and I have some configuration
I have a small VB.NET application that I'm working on using the full version
I'm working on a small web application that changes the contents of a select
I am working on a small application in VB.NET. The program needs administrator privilege
I am working on a small team of web application developers. We edit JSPs
I'm working on a relatively small asp.net web application and am wondering if there
My situation is this: I have an ASP.NET web application that I want to
I am working on an ASP.NET web application, we are a small team (4
I would like to add some scripting support to a Silverlight 4 application that

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.