Search code examples
javaandroidsipjava-threads

Java runOnUiThread and Thread.sleep


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.


Solution

  • 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);