子线程创建处理程序的作用是什么?

What is the role of the child thread creation handler?

1.I 在子线程中创建处理程序时遇到问题

喜欢

public class Main4Activity extends Activity {

private TextView mTextView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextView = (TextView) findViewById(R.id.text_view);
    new Thread() {
        @Override
        public void run() {
            Looper.prepare();
            @SuppressLint("HandlerLeak") Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    Toast.makeText(Main4Activity.this, "handler msg", Toast.LENGTH_SHORT).show();
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    mTextView.setText("100");
                }
            };
            handler.sendEmptyMessage(1);
            Looper.loop();
        }
    }.start();
}

} 上面的代码会崩溃。

Process: com.example.hellokai.kotlindemo, PID: 27485
                                               android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                                                   at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6986)
                                                   at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1074)
                                                   at android.view.View.requestLayout(View.java:19889)
                                                   at android.view.View.requestLayout(View.java:19889)
                                                   at android.view.View.requestLayout(View.java:19889)
                                                   at android.view.View.requestLayout(View.java:19889)
                                                   at android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:1959)
                                                   at android.view.View.requestLayout(View.java:19889)
                                                   at android.widget.TextView.checkForRelayout(TextView.java:7369)
                                                   at android.widget.TextView.setText(TextView.java:4480)
                                                   at android.widget.TextView.setText(TextView.java:4337)
                                                   at android.widget.TextView.setText(TextView.java:4312)
                                                   at com.example.hellokai.kotlindemo.Main4Activity.handleMessage(Main4Activity.java:40)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:154)
                                                   at com.example.hellokai.kotlindemo.Main4Activity.run(Main4Activity.java:45)

2.I知道在主线程更新ui,handler的创建在主线程创建,然后在子线程发送消息给handler就可以更新Ui.

3.My问题是在子线程中创建的handler有什么作用?我们什么时候需要这样做?场景有什么用?

希望有人能解决我的困惑!

  1. I have a problem about creating a handler in a child thread.

您正在从后台线程更新 ui。

例如,您可以从线程发送消息并更新 ui 喜欢

private Handler handler = new Handler(Looper.getMainLooper()) {
    @Override
    public void handleMessage(Message msg) {
        Toast.makeText(Main4Activity.this.getApplicationContext(), "handler msg", Toast.LENGTH_SHORT).show();

        mTextView.setText((String)msg.obj);
    }
};

然后

  new Thread() {
        @Override
        public void run() {

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Message msg = Message.obtain(); // Creates an new Message instance
            msg.obj = "Hello";
            handler.sendMessage(msg);

        }
    }.start();

或者,如果您只是想要延迟,则不需要线程、循环器和睡眠,正如 pskink https://developer.android.com/reference/android/os/Handler.html

的评论中已经指出的那样
  1. I know to update ui in the main thread,handler creation in the main thread to create, and then send a message in the child thread to the handler can update Ui.

是的,你是对的,你可以在 ui 线程上创建处理程序,你可以从线程发送消息并更新你的 ui

  1. My question is what is the role of the handler created in the child thread?

处理程序与线程的循环程序关联。如果您在 ui 线程中有一个处理程序,则它与之相关联。在您的情况下,您将它放在一个线程中,因此处理程序与该线程循环程序相关联。

When do we need to do that? What is the use of the scene?

当您想从后台线程与 ui 线程或从 ui 线程与后台线程进行通信时。

您的代码

new Thread() {
    @Override
    public void run() {
        Looper.prepare();
        @SuppressLint("HandlerLeak") Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                Toast.makeText(Main4Activity.this, "handler msg", Toast.LENGTH_SHORT).show();
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mTextView.setText("100");
            }
        };
        handler.sendEmptyMessage(1);
        Looper.loop();
    }
}.start();

可以重写以使用 Android api:

new Handler(getMainLooper()).postDelayed(
new Runnable() {
    @Override
    public void run() {
                Toast.makeText(Main4Activity.this, "handler msg", Toast.LENGTH_SHORT).show();
                mTextView.setText("100");
        }
    }
}, 200);

另请注意,您的主要问题是您在工作线程中的 Runnable 中创建了 Handler,您也可以在 onCreate 的早期创建它。