Java runOnUiThread and Thread.sleep

I have this method from a separate class in which 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 into the "if" condition, where I put Thread.sleep. It waits 10 seconds before the code below is executed.

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

I think there is something missing here regarding Thread.sleep. I just want to get rid of it, but I'm not sure about the alternative. Help. Thank.

+4
source share
1 answer

Use the Handler instead of putting the thread to sleep.

So instead if(true) {.....}try the following:

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

All Articles