[英]Call a method every second android
Is there a way to call a method every seconds like update in unity3d??有没有办法像unity3d中的update那样每秒钟调用一个方法?? I have tried this: but it give me many error on handler
我试过这个:但它给了我很多关于处理程序的错误
Handler h = new Handler();
int delay = 100; //milliseconds
h.postDelayed(new Runnable(){
public void run(){
//do something
h.postDelayed(this, delay);
}
}, delay);
You can use Java Threads
:您可以使用 Java
Threads
:
public static void main(String[] args)
{
Runnable myRunnable = new Runnable(){
public void run(){
while(true)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread thread = new Thread(new Runnable(){
public void run(){
System.out.println("Run...");
}
});
thread.start();
}
}
};
Thread thread = new Thread(myRunnable);
thread.start();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.