在Android中每10秒显示一次数据 [英] display data after every 10 seconds in Android

查看:37
本文介绍了在Android中每10秒显示一次数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须每 10 秒显示一些数据.谁能告诉我怎么做?

I have to display some data after every 10 seconds. Can anyone tell me how to do that?

推荐答案

还有另一种方法可用于在特定时间间隔更新 UI.以上两个选项都是正确的,但取决于具体情况,您可以使用替代方法在特定时间间隔更新 UI.

There is an another way also that you can use to update the UI on specific time interval. Above two options are correct but depends on the situation you can use alternate ways to update the UI on specific time interval.

首先为Handler声明一个全局变量,用于从Thread更新UI控件,如下图

First declare one global varialbe for Handler to update the UI control from Thread, like below

Handler mHandler = new Handler();

现在创建一个线程并使用while循环通过线程的sleep方法周期性地执行任务.

Now create one Thread and use while loop to periodically perform the task using the sleep method of the thread.

 new Thread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(10000);
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            // Write your code here to update the UI.
                        }
                    });
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }
    }).start();

这篇关于在Android中每10秒显示一次数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆