Android的线程和处理程序不工作 [英] Android threading and Handler not working

查看:118
本文介绍了Android的线程和处理程序不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近重构一个老项目,发现一个特定部分不再愿意妥善无论我做什么运行。从本质上讲,我有一个TextView一个活动。这种观点在一定的时间间隔从同一个类中调用一个线程更新。暂停与视频下载完成和一个Handler用于触发用户界面的更新。关键是,我现在要么得到一个CalledFromWrongThreadException说我不能操纵从另一个线程,过了好一会之后的所有文字击中立即查看,或看似立即触发关闭,不睡在所有的视图。我曾previously做过像这样(相关部分包括):

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();
 }
}

这是我使用的是什么previously的要点,这工作得很好。但是,我真的不喜欢设置类变量,然后有处理程序后抓住它的做法。我修改它来传递消息设置为一个字符串,然后将去皮并追加到的TextView的.OBJ领域。这将炸毁与上述异常。我试图留在原位的处理程序声明,但内另一种方法移动逻辑,其中处理程序将.POST(可运行(){});具有相同的胆,但是这将只是失速和后一个显著等待其追加。我不是处理程序和Java / Android的线程内的内部运作非常精明的,所以任何指针将是非常欢迎的。

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.

推荐答案

您是静态创建处理程序,它创建它在装载机线程。创建的onCreate()处理程序,它会被绑定到UI线程,并解决您的问题。此外,而不是视频下载,你可以做<一个href=\"http://developer.android.com/reference/android/os/Handler.html#sendEmptyMessageDelayed%28int,%20long%29\"相对=nofollow> handler.sendEmptyMessageDelayed 每当你收到一条消息,只是发送初始消息,踢东西了。我认为视频下载在处理程序可以阻塞线程。

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.

这篇关于Android的线程和处理程序不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆