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

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

How can I display Toast messages from a thread?

相关标签:
11条回答
  • 2020-11-22 05:45

    Like this or this, with a Runnable that shows the Toast. Namely,

    Activity activity = // reference to an Activity
    // or
    View view = // reference to a View
    
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            showToast(activity);
        }
    });
    // or
    view.post(new Runnable() {
        @Override
        public void run() {
            showToast(view.getContext());
        }
    });
    
    private void showToast(Context ctx) {
        Toast.makeText(ctx, "Hi!", Toast.LENGTH_SHORT).show();
    }
    
    0 讨论(0)
  • 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();
        }
    });
    
    0 讨论(0)
  • 2020-11-22 05:50

    I encountered the same problem:

    E/AndroidRuntime: FATAL EXCEPTION: Thread-4
                  Process: com.example.languoguang.welcomeapp, PID: 4724
                  java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()
                      at android.widget.Toast$TN.<init>(Toast.java:393)
                      at android.widget.Toast.<init>(Toast.java:117)
                      at android.widget.Toast.makeText(Toast.java:280)
                      at android.widget.Toast.makeText(Toast.java:270)
                      at com.example.languoguang.welcomeapp.MainActivity$1.run(MainActivity.java:51)
                      at java.lang.Thread.run(Thread.java:764)
    I/Process: Sending signal. PID: 4724 SIG: 9
    Application terminated.
    

    Before: onCreate function

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getBaseContext(), "Thread", Toast.LENGTH_LONG).show();
        }
    });
    thread.start();
    

    After: onCreate function

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getBaseContext(), "Thread", Toast.LENGTH_LONG).show();
        }
    });
    

    it worked.

    0 讨论(0)
  • 2020-11-22 05:52
    1. Get UI Thread Handler instance and use handler.sendMessage();
    2. Call post() method handler.post();
    3. runOnUiThread()
    4. view.post()
    0 讨论(0)
  • 2020-11-22 05:56

    You can use Looper to send Toast message. Go through this link for more details.

    public void showToastInThread(final Context context,final String str){
        Looper.prepare();
        MessageQueue queue = Looper.myQueue();
        queue.addIdleHandler(new IdleHandler() {
             int mReqCount = 0;
    
             @Override
             public boolean queueIdle() {
                 if (++mReqCount == 2) {
                      Looper.myLooper().quit();
                      return false;
                 } else
                      return true;
             }
        });
        Toast.makeText(context, str,Toast.LENGTH_LONG).show();      
        Looper.loop();
    }
    

    and it is called in your thread. Context may be Activity.getContext() getting from the Activity you have to show the toast.

    0 讨论(0)
提交回复
热议问题