postDelayed里面while循环不起作用?

时间:2017-08-25 03:17:12

标签: java android loops while-loop handler

我希望在更新消息之前延迟应用程序以使其看起来更逼真。 当我试图延迟更新一条消息时,它可以正常工作,如下所示

if (currentMessage.getMessageStatue() == MESSAGE_RECEIVED) {
                    handler = new Handler();
                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            updateMessage();
                            notifyAdapter();
                            currentMessage++;
                        }
                    };
                    handler.postDelayed(runnable, 3000);
                }

但问题是,当我试图循环抛出每条消息并执行相同的任务时,应用程序会继续延迟并且android强制它停止。这就是我想要做的事情:

while (currentMessage.getMessageStatue() == MESSAGE_RECEIVED) {
                    handler = new Handler();
                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            updateMessage();
                            notifyAdapter();
                            currentMessage++;
                        }
                    };
                    handler.postDelayed(runnable, 3000);
                }

我可以克服这个问题并在while循环中使用postDelayed处理程序吗?

0 个答案:

没有答案