每隔x秒退出一次



大家好,一旦满足特定标准,我将尝试阻止一个方法每6秒执行一次操作。我想使用finish(),但它将我重定向回上一个布局,并且仍然每6秒显示一次toast消息。这是的运行方法

protected static final long TIME_DELAY = 6000;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);
        mHandler.post(updateTextRunnable);
    }
    Runnable updateTextRunnable=new Runnable(){  
        public void run() {       
            if(//Criteria==true){
                     message();
                         finish(); 
                }
                  mHandler.postDelayed(this, TIME_DELAY);  
                 }  
         };  
public void message(){
        Toast.makeText(this, "Found",
                Toast.LENGTH_SHORT).show();
    }

一旦满足条件,我怎么能让它退出运行,但仍然保持在相同的布局

只有在条件为false时才调用postDeleyd调用(即添加如下的else语句)。

if(//Criteria==true){    
    message();   
    finish();  
} else {    
    mHandler.postDelayed(this, TIME_DELAY);   
}

最新更新