how are you I like to do an event every Sunday of the year at 10:00:00 AM It works one time.
Example: A countdown timer ends on Saturday, and on Sunday at 10:00:00, i restarts itself automatically at ten o'clock (The interval is one day On this interval day I would like to open a gift after the day it closes)
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.set(Calendar.MINUTE, 19);
cal.set(Calendar.SECOND, 20);
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
if (cal.get(Calendar.HOUR_OF_DAY) == 8) {
Toast.makeText(getApplicationContext(), "synday is 8 oclock", Toast.LENGTH_SHORT).show();
if (cal.get(Calendar.MINUTE) == 19) {
Toast.makeText(getApplicationContext(), "synday is min", Toast.LENGTH_SHORT).show();
resetTimer();
} else {
Toast.makeText(getApplicationContext(), "synday not min", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "synday no 8", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "no sunday", Toast.LENGTH_SHORT).show();
}
}
If you want to schedule the timer reset every Saturday, you can use Android's WorkManager. It supports every API level so you shouldn't have any issues. Here is the docs: https://developer.android.com/topic/libraries/architecture/workmanager
it boils up to this:
import these dependencies
implementation "androidx.work:work-runtime:2.4.0"
Define what you want to do:
public class TimerResetWorker extends Worker {
public TimerResetWorker(@NonNull Context context,@NonNull WorkerParameters params) {
super(context, params);
}
@Override
public Result doWork() {
resetTimer(); //do what you want to do
return Result.success(); //you have Result.failure() / retry() in case something goes wrong
}
}
Then just schedule the job every 7 days (starting from a Saturday):
PeriodicWorkRequest mWork = new PeriodicWorkRequest.Builder(TimerResetWorker.class, 7, TimeUnit.DAYS).build();
WorkManager.getInstance().enqueue(mWork);