0

When I call the function() I want to have a chance to cancel the SOS call, If I click the button. Basically It reads Integer.parseInt(cancelTime.getText().toString()) that has a time in second to be able to cancel.

My problem is that I'm trying to show like a countdown of the time elapsed from Integer.parseInt(cancelTime.getText().toString()) to 0 and it appears a huge number: ex: 10546468261

private void function()
{    

    startTime = System.currentTimeMillis();

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            double elapsedTime = (System.currentTimeMillis() - startTime)/1000;
            alertButton.setText("CANCEL THE SOS: " + (int)elapsedTime);
            alertButton.setBackgroundColor(Color.parseColor("#FF0000"));
        }
    };

    Handler handler = new Handler();
    handler.postDelayed(runnable, Integer.parseInt(cancelTime.getText().toString()));
}
EKN
  • 1,886
  • 1
  • 16
  • 29

1 Answers1

0

I have modified your code, This may help you.

private void function()
{    

     new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
     alertButton.setText("seconds remaining: " + millisUntilFinished / 1000);
     alertButton.setBackgroundColor(Color.parseColor("#FF0000"));
     }

     public void onFinish() {
         alertButton.setText("Cancel SOS");
     }
  }.start();
}

Modify new CountDownTimer(30000, 1000) as per your need

Parameter 1:- long millisInFuture

Parameter 2:- long countDownInterval

EKN
  • 1,886
  • 1
  • 16
  • 29