当进度条状态达到100时,我正在尝试运行一些代码段。(在本例中为diggingStatus) 但由于某种原因,Log.d代码被调用2次而不是1次。我猜我的if(diggingStatus == 100)条件是错误的。
new Thread(new Runnable() {
private Handler diggingHandler = new Handler();
@Override
public void run() {
while(diggingStatus < 100){
// Update the progress status
diggingStatus +=1;
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
// Update the progress bar
diggingHandler.post(new Runnable() {
@Override
public void run() {
diggingbar.setProgress(diggingStatus);
if(diggingStatus==100){
//Run this code when progress is completed.
Log.d("i got called","i got called "); //Why this is called 2 times?
}
}
});
}
}
}).start();
答案 0 :(得分:0)
由于您处理异步消息,当两者执行变量diggingStatus已经是100时。
你必须将进度数量发送给处理程序,例如:
final int progress = diggingStatus; //get only the current
// Update the progress bar
diggingHandler.post(new Runnable() {
@Override
public void run() {
diggingbar.setProgress(progress);
if(progress==100)
Log.d("i got called","i got called ");
}
});