Java runOnUiThread和Thread.sleep

我的这个方法来自一个单独的类,其中当调用结束时,我的ImageView的颜色从红色变为白色。示例代码如下:

public void endOfCall(){

    ((Activity)mContext).runOnUiThread(new Runnable(){
        @Override
        public void run(){
            TargetDetails.oncall.setVisibility(View.VISIBLE);
            TargetDetails.endcall.setVisibility(View.GONE);
        }
    });

    try{
        call.endCall();
    }catch (SipException se) {}

    call.close();

    //this is just a representation; not the actual code
    if(true){
      Thread.sleep(10000);
    }

    //new intent here
}

问题出现在我放置Thread.sleep的‘if’条件时。它会等待10秒,然后执行以下代码

TargetDetails.oncall.setVisibility(View.VISIBLE);
TargetDetails.endcall.setVisibility(View.GONE);

我想我在这里遗漏了一些关于线程的东西。我只想摆脱它,但我不确定除此之外还有其他选择。帮助。谢谢。


解决方案

使用Handler而不是将线程置于休眠状态。

所以不要if(true) {.....}试试:

Handler h = new Handler();
h.postDelayed(new Runnable() {
    @Override public void run() {
        //new intent here
    }
}, 10000);

相关文章