为什么使用处理程序? [英] Why use Handler?

查看:102
本文介绍了为什么使用处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个非常基本的处理程序的教程碰到这个code。在code为工作正常,但我不明白为什么我要使用处理程序 progressDialog.dismiss() ???我删除了处理器的一部分,放在 progressDialog.dismiss()运行()方法,它工作得很好。那么,为什么使用的处理器???

 进口android.app.Activity;
    进口android.app.ProgressDialog;
    进口android.os.Bundle;
    进口android.os.Handler;
    进口android.os.Message;
    进口android.view.View;
    进口android.view.View.OnClickListener;
    进口android.widget.Button;
    进口android.widget.Toast;


    公共类HandlerThread延伸活动{

    私人键式启动;
    私人ProgressDialog progressDialog;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        启动=(按钮)findViewById(R.id.Button01);
        start.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(查看为arg0){
                // TODO自动生成方法存根
                fetchData();
            }

        });
    }



    保护无效fetchData(){
        // TODO自动生成方法存根
        progressDialog = ProgressDialog.show(这一点,,做......);
        新的Thread(){
            公共无效的run(){
                尝试 {

                    视频下载(8000);

                    }赶上(InterruptedException异常E){

                    }
                      messageHandler.sendEmptyMessage(0);

                    }
        }。开始();


    }



    私人处理程序的MessageHandler =新的处理程序(){

        公共无效的handleMessage(信息MSG){
            super.handleMessage(MSG);
            progressDialog.dismiss();

        }
    };
}
 

解决方案

从文档查看

  

您必须始终呼吁任何任何方法时一定要在UI线程   视图。的如果你正在做的其他线程的工作,并希望更新   从该线程的视图状态,你应该使用处理程序

在您的例子,当你调用辞退() ProgressDialog ,按法上述文件,则必须从UI线程操作。该的MessageHandler 初始化为处理程序 HandlerThread 类实例化(在UI线程presumably)。

处理程序的文档

  

每个处理程序实例都与一个单独的线程和相关   线程的消息队列。当你创建一个新的处理程序,势必   这是创建它的线程的线程/消息队列 - 从   上,它将提供的消息和可运行到该信息,即点   排队并执行他们,因为他们出来的消息队列。

因此​​,要与你的新的线程UI线程沟通,只是发布消息处理程序 UI线程创建的。

如果你在一个查看从UI线程之外,它调用的未定义行为的,这意味着,它的可能会出现方法的做工精细。但它不是的总是的保证做工精细。

I came across this code in a very basic Handler tutorial. The code is working fine but I do not understand why I have to use Handler for progressDialog.dismiss() ??? I removed the Handler part and placed progressDialog.dismiss() in the run() method and it worked fine. So why used Handler???

 import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;


    public class HandlerThread extends Activity{

    private Button start;
    private ProgressDialog progressDialog;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        start = (Button) findViewById(R.id.Button01);
        start.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                fetchData();
            }

        });
    }



    protected void fetchData() {
        // TODO Auto-generated method stub
        progressDialog = ProgressDialog.show(this, "", "Doing...");
        new Thread() {
            public void run() {
                try {

                    Thread.sleep(8000);

                    } catch (InterruptedException e) {

                    }
                      messageHandler.sendEmptyMessage(0);

                    }
        }.start();


    }



    private Handler messageHandler = new Handler() {

        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            progressDialog.dismiss();

        }
    };
}

解决方案

From the documentation of View:

You must always be on the UI thread when calling any method on any view. If you are doing work on other threads and want to update the state of a view from that thread, you should use a Handler.

In your example, when you've to call the dismiss() method on the ProgressDialog, as per the above documentation, you must do so from the UI thread. The messageHandler is initialized to an instance of a Handler when the HandlerThread class is instantiated (presumably on the UI thread).

From the documentation of Handler:

Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

So to communicate with the UI thread from your new thread, just post a message to the Handler created on the UI thread.

If you call methods on a View from outside the UI thread, it invokes undefined behaviour, which means, it may appear to work fine. But it's not always guaranteed to work fine.

这篇关于为什么使用处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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