0

I need to send a SMS in a future time, i.e. in 5 minutes, and to show in the UI the remaining time in the format 00:00.

My first choice was using android-alarm, but I dont know how to show in the layout the countdown.

Next, I tried to use a chronometer, and use the view of the object, but the time is always up, so i'd need to make a lot of math operations to refresh the view.

Finally I've used a CountDownTimer, and i show in a TextView the elapsed time.

That is the best choice?

Here is a short of code:

public void startCountDown(View v) {

    if (!activedCountDown) {

        activedCountDown = true;

        final TextView mTextField = (TextView) findViewById(R.id.mTextField);

        EditText text = (EditText) findViewById(R.id.etMinutos);

        String mins = text.getEditableText().toString();

        futureTime = Integer.parseInt(mins) * 60000;

        isTheFinalCountDown = new CountDownTimer(futureTime, interval) {

            @Override
            public void onTick(long millisUntilFinished) {

                if (millisUntilFinished  < 60000) {
                    mTextField.setText("00:" + millisUntilFinished / 1000);
                } else {
                    //TODO parse the textfield to show minutes and seconds
                }

            }

            @Override
            public void onFinish() {
                //TODO: launch SMS
                mTextField.setText("Send SMS now");
                activedCountDown = false;
            }
        }.start();
    }
}

1 Answers1

0

To show user time remaining you may use the countdown timer
BUT FOR SENDING SMS
I will strongly discourage you to use it, you must use the the Alarm/Broadcast reciever because your activity variables might get freed from memory as Android OS needs memory if other apps are running.

See this for detail, it happened to me.

Community
  • 1
  • 1
Salmaan
  • 3,543
  • 8
  • 33
  • 59
  • Thank you very much. This project is like 'panic button' app, but i need to provide to the user the possibility of to activate a sending a SMS in a time if he/she doesnt cancel the countdown. If the user is going to do a risk task/operation (for example work on high voltage) would be helpful. – José Manuel Gálvez Apr 10 '15 at 10:25
  • Well @JoséManuelGálvez that means user will not have any chance of browsing/using other apps after launching other apps. Which means your app variables wont be freed from the memory. If this is the case, you can go on. But broadcast receiver is the preferred method, you can also cancel it. – Salmaan Apr 10 '15 at 11:54