有没有办法为进度条设置计时器?例如等待10秒然后关闭进度条?如果有人能给我一个简单的代码,我将不胜感激。
非常感谢提前:)
答案 0 :(得分:2)
您可以在UI-Thread中创建Handler,然后通过sendMessageDelayed调用它。
...
final Handler h = new Handler() {
@Override
public void handleMessage(Message message) {
mProgressBar.dismiss();
}
};
h.sendMessageDelayed(new Message(), 10000);
...
此代码不是testet。
答案 1 :(得分:1)
使用Handler课程。
import android.os.Handler;
import android.os.Message;
public class someClass implements Handler.Callback {
public static final int MSG_HIDE_PBAR = 0;
static final long PBAR_DELAY = 10 * 1000; //Delay is is milliseconds
//Probably should initialize this in your creation code (onCreate if activity)
//and likewise set it to null when destroyed (onDestroy if activity)
private Handler mHandler = new Handler(this);
public void showPbar(){
//Show your progress bar here
mHandler.sendEmptyMessageDelayed(MSG_HIDE_PBAR, PBAR_DELAY);
}
public boolean handleMessage(Message msg){
switch (msg.what){
case MSG_HIDE_PBAR:
//hide your progress bar here, call postInvalidate instead of
//invalidate because we are in a different thread
break;
}
return true;
}
}
快速:是的,脏的:是的,完成工作:是的
你可以通过一个带有睡眠呼叫的AsyncTask或一个检查时钟的循环完成相同的操作。 Handler
对我来说似乎更清晰。
答案 2 :(得分:0)
是的,您可以使用Thread
和runOnUiThread在10秒后解雇:
public void myThread() {
Thread th = new Thread() {
@Override
public void run() {
try {
while (mRunning) {
Thread.sleep(10L);//10s wait
YourCurrentActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//DISMISS PROGRESS BAR HERE
mRunning = false;
}
});
}
} catch (InterruptedException e) {
// TODO: handle exception
}
}
};
th.start();
}