1

I need to calculate an interval of 10 seconds. Why does does the "IF" clause not get triggered?

public void updatePlayTimer() {
        timeTextView.setText(radioService.getPlayingTime());

        final Handler handler = new Handler();
        Timer timer = new Timer();
        TimerTask doAsynchronousTask = new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        timeTextView.setText(radioService.getPlayingTime());

                        String demo=radioService.getPlayingTime();
                        if (demo=="00:10"){
                            radioService.stop();
                        }

                    }
                });
            }
        };
        timer.schedule(doAsynchronousTask, 0, 1000);
    }
DanGar
  • 3,018
  • 17
  • 17
waplay
  • 57
  • 1
  • 3
  • 2
    `"Why does not the condition for "IF"?"` I don't know what you're asking. *What about* the `if` statement? – Jonathon Reinhart Nov 19 '13 at 04:28
  • (That post explains why the `==` will *never* work there. Although you might want to do something else with the data: does get playing time *always* return a string in that format?) – user2864740 Nov 19 '13 at 04:32

1 Answers1

2

Try equals() instead of == to compare the strings.

String demo = radioService.getPlayingTime();
if (demo.equals("00:10")) {
    radioService.stop();
}

This checks that the actual content of the strings are equal, whereas the == operator checks if the references to the objects are equal.

ashatte
  • 5,442
  • 8
  • 39
  • 50