2

I am trying to write a message to stdout in android every second for which a given button is held(I intend to place a method there later). I use switch in a standard onTouch method to detect button press:

protected void setFab(FloatingActionButton fab) {
    fab.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    System.out.println("Button pressed");
                    handleButtonDown();
                    return true;
                }
                case MotionEvent.ACTION_UP: {
                    System.out.println("Button released");
                    handleButtonUp();
                    return true;
                }
                default:
                    return true;
            }
        }
    });
} 

And global two global booleans:

private boolean captureThreadRunning = false;
private boolean cancelCaptureThread = false;

Which are to stop the looping when the button is released:

public void handleButtonUp() {
    cancelCaptureThread = true;
}

However, when I start the worker thread, it gets stuck in an infinite loop, even when the button is released and thus the global boolean should be changed:

public void handleButtonDown() {
    System.out.println("Capture thread running: " + captureThreadRunning);
    if (!captureThreadRunning) {
        System.out.println("Thread starting");
        startCaptureThread();
    }
}

public void startCaptureThread() {
    System.out.println("Thread started");
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                captureThreadRunning = true;
                System.out.println("Got to try");
                while (!cancelCaptureThread) {
                    System.out.println("Success");
                    try {
                        System.out.println("Falling asleep");
                        Thread.sleep(1000);
                        System.out.println("Slept 1000");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(
                                "Interrupted.", e);
                    }
                }
            } finally {
                System.out.println("got to finally");
                captureThreadRunning = false;
                cancelCaptureThread = false;
            }
        }
    });
    thread.run();
}

Not only that, but the UI also gets frozen, which it certainly shouldn't, cause I am doing everything in a separate thread. When I release the button, the loop should halt, because the boolean gets changed. I am quite new to both threads and android, so I guess I am just missing something.

rozsatib
  • 23
  • 5

1 Answers1

0

But you are indeed executing your code in your main thread. Notice that you are calling

thread.run();

so your execution path enters into an infinite loop. Change it to

thread.start();

That will start a new Thread

Alberto S.
  • 7,409
  • 6
  • 27
  • 46