Java runOnUiThread and Thread.sleep

☆樱花仙子☆ 提交于 2019-12-12 11:23:18

问题


I have this method from a separate class wherein when the call ends, the color of my ImageView changes from red to white. Sample code below:

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
}

The problem starts when it goes to the 'if' condition where I put the Thread.sleep. It waits 10seconds before the code below gets executed

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

I think I am missing something here regarding the Thread.sleep. I just wanna get rid of it but I'm not sure of any alternative aside from that. Help. Thanks.


回答1:


Use Handler instead of putting thread to sleep.

So instead of your if(true) {.....} try this:

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


来源:https://stackoverflow.com/questions/29654421/java-runonuithread-and-thread-sleep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!