因为我需要做一些其他的错误检查,所以我已经将其更改为使用处理程序。然而,另一种方式确实有效
mHandler.postdelayed(run, delay);
Runnable run = new Runnable(){
public void run()
{
//run function
}
};
我想到了。我创建了一个新线程来运行计时器并在更新屏幕的主活动中调用一个函数。调用更新屏幕的函数调用 usingrunOnUiThread()
以便它具有更改屏幕的权限。
public class ImageTimer implements Runnable{
private long dur, cur,start;
private boolean needed, run;
private Activity mAct;
ImageTimer(boolean running, long duration, Activity act)
{
run = running;
dur = duration;
needed = false;
mAct = act;
}
@Override
public void run()
{
while(run)
{
if(needed)
{
start = System.currentTimeMillis();
cur = System.currentTimeMillis() - start;
while(cur < dur)
{
cur = System.currentTimeMillis() - start;
}
needed = false ;
mAct.runOnUiThread(new Runnable()
{
public void run()
{
MainActivity.ImageDone();
}
});
}
}
}