Toast是否可以在非UI线程显示?

Toast是可以在非UI线程显示的,请看如下代码(包含在子线程创建Handler)

public class MainActivity extends AppCompatActivity {
    private Handler subHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(MainActivity.this, "MainActivity :" + Thread.currentThread().getName(), Toast.LENGTH_SHORT).show();
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                subHandler = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        Toast.makeText(MainActivity.this, "subHandler :" + Thread.currentThread().getName(), Toast.LENGTH_SHORT).show();
                        subHandler.sendEmptyMessageDelayed(0, 4000);
                    }
                };
                subHandler.sendEmptyMessageDelayed(0, 3000);
                Looper.loop();
            }
        }.start();
    }
}

分别弹出Toast MainActivit :main和subHandler :Thread-3456(此为线程ID,随机值)
所以Toast是可以在非UI线程显示的

你可能感兴趣的:(Toast是否可以在非UI线程显示?)