当前位置:网站首页>Handler asynchronous message processing

Handler asynchronous message processing

2022-06-25 10:22:00 Peach doesn't come out

to update UI Must operate in the main thread , If it is updated in a non main thread UI Will report a mistake :

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

reason : Updated on non main thread UI, as follows :

Solution :handler Asynchronous thread

package com.tzbc.databindtest;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "tzbc";
    private static final int MSG_UPDATE = 415;

    private Button button;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what == MSG_UPDATE){
                button.setText("new Thread");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.btUpdate);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.sendEmptyMessage(MSG_UPDATE);
            }
        });
    }


}

When other contents need to be carried along , Make a statement message:

package com.tzbc.databindtest;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "tzbc";
    private static final int MSG_UPDATE = 415;

    private Button button;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == MSG_UPDATE) {
                button.setText(msg.obj.toString());
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.btUpdate);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String updateStr = "2020/04/15";
                Message message = Message.obtain();
                message.what = MSG_UPDATE;
                message.obj = updateStr;
                handler.sendMessage(message);
            }
        });
    }


}

 

原网站

版权声明
本文为[Peach doesn't come out]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200545120976.html