android infinite loop decision task

Project encountered a need, if isRegister boolean value is true, the need to constantly perform a delay to do logic implementations with Handler + runnable

  Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                if (isRegister){
                   /*执行一段逻辑*/
           			// add your code;
                    handler.postDelayed(this,500);//延时五百毫秒,再次执行这个runnable,如果isRegister为false了就停止执行了
                }
            }
        };
       
        //然后在你初次调起这个延时逻辑的地方调用以下语句:
        handler.postDelayed(runnable,500);//延时五百毫秒,执行runnable

Guess you like

Origin blog.csdn.net/weixin_43115440/article/details/90670706