Aury0n
Aury0n

Reputation: 217

Android stop loop by click button

i am trying to make a button that when its clicked , it changes its color image and starts a countdowntimer in a method activeDelay() as here:

 piscaAutoButton = (Button) rootView.findViewById(R.id.piscaAutoButton);
        piscaAutoButton.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(final View view) {
                if (sessionManager.getPisca()) {
                    sessionManager.setPisca(false);
                    trigger = false;
                    piscaAutoButton.setBackgroundResource(R.drawable.button_bg_round);
                } else {
                    sessionManager.setPisca(true);
                    piscaAutoButton.setBackgroundResource(R.drawable.button_add_round);
                    trigger = true;
                    activeDelay(trigger);

                }

here is my activeDelay method:

private boolean activeDelay(boolean trigger) {
        while (trigger) {        // LOOP WHILE BUTTON IS TRUE CLICKED
            int timerDelay = manualControl.getDelayPisca(); //input for timer
            //delay manual
            new CountDownTimer(timerDelay * 1000, 1000) {
                public void onFinish() {
                    System.out.println("sent");
                    try {
                        System.out.println("blink button " + manualControl.getBlinkButton());
                        if (!manualControl.getBlinkButton().isEmpty()) {
                            MenuActivity.mOut.write(manualControl.getBlinkButton().getBytes());
                        }

                    } catch (IOException e) {
                        e.printStackTrace();

                    }
                }

                public void onTick(long millisUntilFinished) {

                }

            }.start();

        }
        return trigger;
    }

My problem is that i need the counter keeps going after finished, stopping just when the user clicks again in the button (trigger = false). I am having problems to program that, if someone could help,i know the return inside activeDelay ejects from the method, how can we solve that ,tks

Upvotes: 0

Views: 528

Answers (1)

Himanshu arora
Himanshu arora

Reputation: 161

I would suggest you to don't use CountDownTimer(this runs for some specific time period) , instead of this you should use Handler(this run infinitely) . i am sending you handler code.

  private Handler handler = new Handler();

  //call this when you want to start the timer .
  handler.postDelayed(runnable, startTime);


    Runnable runnable = new Runnable() {
    @Override
    public void run() {
        // Do here , whatever you want to do(show updated time e.t.c.) .
        handler.postDelayed(this, xyz);  //xyz is time interval(in your case it is 1000)
    }
};

    //Stop handler when you want(In your case , when user click the button)
    handler.removeCallbacks(runnable);

Upvotes: 1

Related Questions