在Android中每10秒显示一次数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2535733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:13:11  来源:igfitidea点击:

display data after every 10 seconds in Android

androidalertdelay

提问by Farha Ansari

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

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

回答by Rahul Patel

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.

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

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

首先为 Handler 声明一个全局变量以从 Thread 更新 UI 控件,如下所示

Handler mHandler = new Handler();

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

现在创建一个 Thread 并使用 while 循环通过线程的 sleep 方法定期执行任务。

 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();

回答by Kiril

Probably the simplest thing to do is this:

可能最简单的事情是这样的:

while(needToDisplayData)
{
    displayData(); // display the data
    Thread.sleep(10000); // sleep for 10 seconds
}

Alternately you can use a Timer:

或者,您可以使用计时器:

int delay = 1000; // delay for 1 sec. 
int period = 10000; // repeat every 10 sec. 
Timer timer = new Timer(); 
timer.scheduleAtFixedRate(new TimerTask() 
    { 
        public void run() 
        { 
            displayData();  // display the data
        } 
    }, delay, period); 

回答by Janusz

Andrahu was on the right track with defining a handler. If you have a handler that calls your update functions you can simply delay the messagesent to the handler for 10 seconds.

Andrahu 在定义处理程序方面走在正确的轨道上。如果您有一个调用更新函数的处理程序,您可以简单地发送到处理程序的消息延迟10 秒。

In this way you don't need to start your own thread or something like that that will lead to strange errors, debugging and maintenance problems.

通过这种方式,您不需要启动自己的线程或类似的东西,那样会导致奇怪的错误、调试和维护问题。

Just call:

只需致电:

 Handler myHandler = new MyUpdateHandler(GUI to refresh); <- You need to define a own handler that simply calls a update function on your gui.
 myHandler.sendMessageDelayed(message, 10000);

Now your handleMessage function will be called after 10 seconds. You could just send another message in your update function causing the whole cycle to run over and over

现在您的 handleMessage 函数将在 10 秒后被调用。您可以在更新功能中发送另一条消息,导致整个循环一遍又一遍

回答by Sunil kumar

There is Also Another way by Using Handler

还有另一种使用处理程序的方法

final int intervalTime = 10000; // 10 sec
Handler handler = new Handler();
handler.postDelayed(new Runnable()  {   
 @Override
 public void run() {
    //Display Data here
   }
 }, intervalTime);

回答by Sunil kumar

There is a Simple way to display some data after every 10 seconds.

有一种简单的方法可以在每 10 秒后显示一些数据。

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_launcher);
  ActionStartsHere();
}

public void ActionStartsHere() {
  againStartGPSAndSendFile();
}

public void againStartGPSAndSendFile() {
   new CountDownTimer(11000,10000) {
        @Override
        public void onTick(long millisUntilFinished) {
           // Display Data by Every Ten Second  
        }

        @Override
        public void onFinish() {
            ActionStartsHere();
        }
    }.start();
}   

回答by Satheeshkumar Somu

Every 10 seconds automatically refreshed your app screen or activity refreshed

每 10 秒自动刷新您的应用程序屏幕或刷新活动

create inside onCreate() method i tried this code will work for me

在 onCreate() 方法中创建我试过这段代码对我有用

Thread t = new Thread() {
@Override
public void run() {
try {
  while (!isInterrupted()) {
    Thread.sleep(1000);
    runOnUiThread(new Runnable() {
      @Override
      public void run() {            
       //CALL ANY METHOD OR ANY URL OR FUNCTION or any view
      }
    });
  }
} catch (InterruptedException e) {
}
}
};t.start();