I have a button and i set a ontouchlistener to continuously execute a method but as i hold on to the button, it stops executing after 1 method call.
my method basically loops from 2 to 4 continuously.
Below is my code snippet.
broadcastButton = (Button) findViewById(R.id.broadcastButton);
broadcastButton.setText("Loop");
broadcastButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
broadcastmode = 1;
schedulePeriodicMethod();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case MotionEvent.ACTION_UP:
broadcastmode = 0;
stopPeriodicMethod();
}
return true;
}
});
public void schedulePeriodicMethod() {
exHandler.postDelayed(execution, 100);
}
public void stopPeriodicMethod() {
exHandler.removeCallbacks(execution);
}
private Runnable execution = new Runnable() {
@Override
public void run() {
connectDevice(flag);
serialSend("C");
flag++;
if (flag > 4)
flag = 2;
}
};
need help on where am i doing wrong that the program is not looping