0

I'd like to create a infinity loop when I push a button and stop it when I release the button, so I create this, but it doesn't stop when I release the button... can you help me?

   public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {

                if (v.getId() == R.id.up) {

                    for(;;){
                    //make my action                     


   if(event.getAction() == MotionEvent.ACTION_UP) {
                            break;
                        }
                        }
                }


    }

        return false;
}

}
  • it doesn't stop because, while you're in the infinite loop, the message queue isn't being processed. It's the message queue which ultimately will raise the event so that it can be processed by your handler. Also, the second event won't alter the "event" parameter because of that but raise a new event which you should catch. I'd use some timer if that's enough for your needs or create a thread if there's no simpler option, but it's hard to tell without seeing the body of the infinite loop. – wonko realtime Oct 29 '14 at 09:51

1 Answers1

0

The infinity loop will drive the EventQueue into no respond to any other Events. So the MotionEvent.ACTION_UP could not be handled also. You should make the event handler method faster. Otherwise the event dispatching thread could hang.

sanigo
  • 625
  • 4
  • 14