Android threading and Handler not working
Asked Answered
I

1

0

I recently refactored an old project and found that a particular portion no longer wants to run properly no matter what I do. Essentially, I have an Activity with a TextView. This view is updated at timed intervals from a thread invoked within the same class. The pause is accomplished with a Thread.sleep and a Handler is used to trigger the update of the UI. The thing is, now I either get a CalledFromWrongThreadException saying that I cannot manipulate the View from another thread, a long pause followed by all the text hitting the View at once, or it seemingly fires off immediately and doesn't sleep at all. I had previously done it like so (relevant parts included):

public class Updated extends Activity implements Runnable {

private TextView textView;
private String text;
private Handler handle = new Handler(){
    @Override
    public void handleMessage(Message msg){
        textView.append(text);
    }
};
//onCreate and other such things...{
    new Thread(this).start();
}

public void run(){
     //A healthy chunk of logic/conditionals
     text = "Blah blah";
     handle.sendEmtpyMessage(0);
     try {
    Thread.sleep(1500l);
 } catch (InterruptedException e) {
    e.printStackTrace();
 }
}

That's the gist of what I was using previously, which worked fine. However, I really didn't like the practice of setting a class variable and then having the Handler grab it later. I modified it to pass a Message with the .obj field set to a String, which would then be peeled out and appended to the TextView. This would blow up with the aforementioned exception. I tried leaving the Handler declaration in place, but moving the logic within to another method where the Handler would .post(Runnable(){}); with the same guts, but that would just stall and append it after a significant wait. I'm not terribly savvy with the inner workings of Handlers and Threads within Java/Android, so any pointers would be quite welcome.

Internationale answered 25/11, 2012 at 6:59 Comment(0)
B
2

You are creating the handler statically, which creates it in the loader thread. Create the handler in onCreate() and it will be bound to the UI thread, and resolve your issue. Also, instead of Thread.sleep, you can do handler.sendEmptyMessageDelayed whenever you receive a message, and just send an initial message to kick things off. I think Thread.sleep in handler can jam a thread.

Bankhead answered 25/11, 2012 at 7:7 Comment(2)
Thanks! That solved the Exception, but I'm still having issues getting it to pause. I don't want to send an empty message, but a message with a String attached that will be displayed. This is now working, but like you said, Thread.sleep isn't working as I'd like. There are a few loops in which the handler is used and it should be run a good dozen times or so. All I get is the first String, then it immediately moves on while that thread continues running in the background even though there's nothing to do. That or it dumps everything and moves on immediately.Internationale
Ah, nevermind. I have it working now. I realized that I need to put the method that runs final checks and determines where to go within the run() method rather than the main thread.Internationale

© 2022 - 2024 — McMap. All rights reserved.