0

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

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.