次のプログラムでハンドラを使用していますが、i = 5のときにハンドラを停止したいが、ハンドラが停止して連続して実行されないようにします。Handler Runnableを停止するには?
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;
}
});
あなたはすでにこの 'handler.removeCallbacks(runnable);'を持っています。http://developer.android.com/reference/android/os/Handler.html – Raghunandan
あなたもカウントダウンを使うことができますタイマーのハンドラまたはタイマータスク – Raghunandan
[ハンドラをキャンセルする、一時停止したプロセス]の複製が可能です(http://stackoverflow.com/questions/4378533/cancelling-a-handler-postdelayed-process) – Varun