您的代码失败,因为您在后台线程中执行睡眠,但是显示数据必须在UI线程中执行。
您必须从runOnUiThread(Runnable)运行displayData或定义处理程序并将消息发送给它。
例如:
(new Thread(new Runnable() { @Override public void run() { while (!Thread.interrupted()) try { Thread.sleep(1000); runonUiThread(new Runnable() // start actions in UI thread { @Override public void run() { displayData(); // this action have to be in UI thread } }); } catch (InterruptedException e) { // ooops } } })).start(); // the while thread will start in BG thread


