Java runOnUiThread和Thread.sleep

时间:2015-04-15 15:30:37

标签: java android sip java-threads

我从一个单独的类中使用此方法,其中当调用结束时,我的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
}

问题在进入'if'条件时开始,我将Thread.sleep置于其中。它在下面的代码执行之前等待10秒

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

我想我在这里遗漏了一些关于Thread.sleep的内容。我只是想摆脱它,但我不确定除此之外的任何其他选择。救命。感谢。

1 个答案:

答案 0 :(得分:5)

使用Handler而不是让线程进入休眠状态。

所以代替你的if(true) {.....}试试这个:

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