为什么我的 TOAST 在 post 处理程序中显示两次?

Why my TOAST is showing twice in post handler?

我只想在progressStatus达到100时显示一次toast消息,为什么执行了两次?感谢您的热心回复。

new Thread(new Runnable() {
                        @Override
                        public void run() {
                            while (progressStatus < 100) {
                                // Update the progress status
                                progressStatus += 1;

                                // Try to sleep the thread for 50 milliseconds
                                try {
                                    Thread.sleep(50);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }       
                                handler.post(new Runnable(){

                                        @Override
                                        public void run() {
                                            pb = dialog.findViewById(R.id.no_connectiondProgressBar);
                                            pb.setProgress(progressStatus);

                                            if (progressStatus == 100) {
                                                
                                                final Toast toast = new Toast(getApplicationContext());
                                                toast.setDuration(Toast.LENGTH_LONG);
                                                View custom_view = getLayoutInflater().inflate(R.layout.custom_toast, null);
                                                toast.setView(custom_view);
                                                toast.show(); 
                                                dialog.dismiss();

                                            }
                                        }


                                    });
                            }
                        }
                    })

将 handler.post() 移动到 while 循环之外

        new Thread(new Runnable() {
        @Override
        public void run() {
            
            while (progressStatus < 100) {
                // Update the progress status
                progressStatus += 1;

                // Try to sleep the thread for 50 milliseconds
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            handler.post(new Runnable(){

                @Override
                public void run() {
                    pb = dialog.findViewById(R.id.no_connectiondProgressBar);
                    pb.setProgress(progressStatus);

                    if (progressStatus == 100) {

                        final Toast toast = new Toast(getApplicationContext());
                        toast.setDuration(Toast.LENGTH_LONG);
                        View custom_view = getLayoutInflater().inflate(R.layout.custom_toast, null);
                        toast.setView(custom_view);
                        toast.show();
                        dialog.dismiss();

                    }
                }


            });
        }
    })