1

I have tried a lot but my textview is not changing.

Timer should automatically add 30 min to current time and it will be my future time event on button. Click

static final long ONE_MINUTE_IN_MILLIS=60000;//millisecs

Calendar date = Calendar.getInstance();
long t= date.getTimeInMillis();
Date afterAdding30Mins=new Date(t + (30 * ONE_MINUTE_IN_MILLIS));

private void countDownStart() {
    runnable = new Runnable() {
        @Override
        public void run() {
            try {
                handler.postDelayed(this, 1000);

                if (!current_date.after(afterAdding30Mins)) {
                    long diff = t+ 1800000;
                    long minutes = (diff / 1000) / 60;
                    long seconds = (diff / 1000) % 60;

                    String timeLeftFormattedm = String.format(Locale.getDefault(), "%02d", minutes);
                    String timeLeftFormatteds = String.format(Locale.getDefault(), "%02d", seconds);
                    //

                    tv_minute.setText(timeLeftFormattedm);
                    tv_second.setText(timeLeftFormatteds);
                } else {
                    linear_layout_1.setVisibility(View.VISIBLE);
                    linear_layout_2.setVisibility(View.GONE);
                    handler.removeCallbacks(runnable);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    handler.postDelayed(runnable, 0);
}

protected void onStop() {
    super.onStop();
    handler.removeCallbacks(runnable);
}
}
James Z
  • 12,209
  • 10
  • 24
  • 44
Hash
  • 147
  • 1
  • 4
  • 14
  • In simple words, when a button is clicked, you want to start a CountDown? You don't need the calendar class, you can do this with the CountDownTimer class. I'll post an answer soon. – Taslim Oseni Jan 27 '18 at 18:50
  • Yes I know that but in countdown timer class I am unable to save state of timer so I am approaching this method, I mean when I close the app timer stops , please suggest me how can. I do it – Hash Jan 27 '18 at 18:52
  • Oh! I understand now.. Why not use a BroadcastReceiver then? Even when the app is closed, everything still works fine! – Taslim Oseni Jan 27 '18 at 18:57
  • Thanks for suggesting but I am completely noob so don't know much about service and reciever , if you have time kindly tell me how can I do that? – Hash Jan 27 '18 at 19:05

1 Answers1

0

What you need is a BroadcastReceiver. With a BroadcastReceiver, you can schedule tasks or events for the future and they'd still run/work even when the app is destroyed/shutdown. In your case, you can call the BroadcastReceiver immediately after your button click and schedule for 30 minutes.

To understand how BroadcastReceivers work: Read through this guide or take a look at this question.

This YouTube video would also give you a headstart,

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
  • Can you please see this question? – Hash Jan 28 '18 at 03:58
  • https://stackoverflow.com/questions/48448539/how-to-save-state-of-countdown-timer-even-if-the-app-is-destroyed?noredirect=1#comment83907024_48448539 – Hash Jan 28 '18 at 03:58
  • Can you give me some video tutorial for broadcast with countdown timer? I am unable to do anything with broadcast reciever – Hash Jan 28 '18 at 05:37