java15handler_java – 如何停止Handler Runnable?

我在以下程序中使用处理程序,我想在i = 5时停止它,但处理程序不会停止并连续运行。

b1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

handler = new Handler();

runnable = new Runnable() {

public void run() {

try {

Toast.makeText(getApplicationContext(), "Handler is working", Toast.LENGTH_LONG).show();

System.out.print("Handler is working");

if(i==5){

//Thread.currentThread().interrupt();

handler.removeCallbacks(runnable);

System.out.print("ok");

Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_LONG).show();

}

i++;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

handler.postDelayed(this, 5000);

}

};

handler.postDelayed(runnable, 5000);

//return;

}

});

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页