I'm implementing a countdown to the Saturday- day of each week, I want that when the counter reaches zero (Saturday) it will be activated again for the next Saturday. But I have no idea about how to update the date each week.
The code I am using is as below:
private void CountDown() {
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
formatter.setLenient(false);
String endTime = "12.03.2018, 15:05:36";
Date endDate;
try {
endDate = formatter.parse(endTime);
milliseconds = endDate.getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startTime = System.currentTimeMillis();
diff = milliseconds - startTime;
mCountDownTimer = new CountDownTimer(milliseconds, 1000) {
@Override
public void onTick(long millisUntilFinished) {
startTime=startTime-1;
Long serverUptimeSeconds =
(millisUntilFinished - startTime) / 1000;
String daysLeft = String.format("%d", serverUptimeSeconds / 86400);
txtdays.setText(daysLeft);
String hoursLeft = String.format("%d", (serverUptimeSeconds % 86400) / 3600);
txthour.setText(hoursLeft);
String minutesLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) / 60);
txtmins.setText(minutesLeft);
String secondsLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) % 60);
txtsec.setText(secondsLeft);
}
@Override
public void onFinish() {
}
}.start();
}
Thanks for the help.
You need alarm manager service in order to do this, with that you can set up weekly schedules for your data.
For more info on how to use AlarmManager please refer to AlarmManager
A quick brief of what it does
Alarms (based on the AlarmManager class) give you a way to perform time-based operations outside the lifetime of your application. For example, you could use an alarm to initiate a long-running operation, such as starting a service once a day to download a weather forecast.
for schedule Alarms please refer to Scheduling Repeating Alarms
Edit: you are using a countdown that will stop if the app is not open, with alarm manager you will solve this as it works in the background.
happy coding.