How do you display a Toast from a background thread on Android?

后端 未结 11 1885
难免孤独
难免孤独 2020-11-22 05:03

How can I display Toast messages from a thread?

11条回答
  •  别那么骄傲
    2020-11-22 05:50

    You can do it by calling an Activity's runOnUiThread method from your thread:

    activity.runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();
        }
    });
    

提交回复
热议问题