0

I need to fire an event when a user holds down a button, and fire another event, when the user releases it in my app. The events are short Asyc Tasks, which run in background.

The problem is, I know how to make this happen with the OnTouchListener(), but it seems like when you hold down the button, it runs an infinite loop on the onTouch() method, stopping when the user releases it. Although since my Tasks are launched only once, It runs over and over empty, doing nothing.

I'm not sure if there is any other way possible to achieve this, which fires my back events and stops with the loop. It would be beneficial, as a lot of resources in the form of side threads are already being consumed, and I would like to save some useless running of a method. Here is my implementation, if something is wrong with it:

private OnTouchListener down = new OnTouchListener() {

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        // TODO Auto-generated method stub

        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            //Async Task
        } else if (action == MotionEvent.ACTION_UP) {
            //Async Task
        }

        return false;
    }
};
Rijul
  • 515
  • 7
  • 19
  • 1
    You can add a longpress gesture listener http://stackoverflow.com/questions/7919865/detecting-a-long-press-with-android – Logi24 Apr 11 '14 at 22:09
  • But that detects a long click. Like, I can fire 1 event when user presses and releases for about maybe 2 sec, if I understand it properly. – Rijul Apr 12 '14 at 01:20

0 Answers0