Here I am doing it using Handler but it only increments a number once. I want it to increment the number until I stop it using handler.removeCallbacksAndMessages(null);.
Can anyone please guide me how can I achieve this. I am just new to android.
Handler handler;
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
count++;
}
}, 1000);
Define a Thread a start it.
Thread counterThread=new Thread(()->{
try{
while(true){
counter++;
Thread.sleep(1000);
}
}
catch (Exception e){
}
});
counterThread.start();
And when you want to stop this thread call counterThread.interrupt();