0

This works: Thread.Sleep(1000)

This does not work: Thread.Sleep(frequency)

The frequency variable takes edittext input from a user and parses it into an integer so it should pass just fine. I have no idea what I'm doing wrong. Help appreciated :)

private void FlashButtonClicked() {
        startPattern = true;
        try{
            frequency = Long.parseLong(frequencyInput.getText().toString());
        }catch(NumberFormatException e){
            Toast.makeText(getBaseContext(), "value not acceptable", Toast.LENGTH_LONG).show();
        }
        frequency = (1 / frequency) * 1000;
        while(startPattern){
            enableTorch();
            try{
                // see if you can figure out why I can't pass the user input into the
                //Thread.sleep() method without it fucking up. When you change it manually with
                // a long input, it works just fine.
                Thread.sleep(frequency);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
}
Kinjal
  • 327
  • 2
  • 11
  • 1
    What do you mean by "does not work"? – Malt Mar 26 '19 at 08:25
  • I am trying to get the camera flash to blink at a regular frequency. When I manually type in the millisecond delay for Thread.Sleep(), it works. When I pass the variable, it doesn't. – Eric Booker Mar 26 '19 at 08:28
  • 1
    What do you mean by "it doens't work"? What happens? – Malt Mar 26 '19 at 08:29
  • The camera flash does not activate at the correct frequency. It just blinks wildly. When I type in the number of milliseconds, it works just fine. Only issue is when I try to pass that variable from the user input EditText widget(Which i cast to a long). – Eric Booker Mar 26 '19 at 08:31
  • You don't need to call `toString()` on a `String`. – user207421 Mar 26 '19 at 11:17

1 Answers1

0

If frequency is of type long and is greater than 1, then 1/frequency is zero (because / means integer division). Replace it with: frequency = 1000/frequency

atarasenko
  • 1,768
  • 14
  • 19