如何使用在某一天结束并在另一天重新开始计数的计数器 (CountDown)?
How to work a counter that ends a certain day and restarts the count on another day (CountDown)?
你好吗
我喜欢在一年中的每个星期日 10:00:00 上午 做一个活动
一次有效
示例:倒数计时器在周六结束,周日在 10:00:00,i 会在十点自动重启( interval是一天 在这个interval day我想在它关闭的那一天之后打开一个礼物)
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();
}
}
如果您想安排每周六重置定时器,您可以使用 Android 的 WorkManager。
它支持每个 API 级别,因此您应该不会有任何问题。
这是文档:https://developer.android.com/topic/libraries/architecture/workmanager
归结为:
导入这些依赖项
implementation "androidx.work:work-runtime:2.4.0"
定义你想做什么:
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
}
}
然后每 7 天安排一次作业(从星期六开始):
PeriodicWorkRequest mWork = new PeriodicWorkRequest.Builder(TimerResetWorker.class, 7, TimeUnit.DAYS).build();
WorkManager.getInstance().enqueue(mWork);
你好吗 我喜欢在一年中的每个星期日 10:00:00 上午 做一个活动 一次有效
示例:倒数计时器在周六结束,周日在 10:00:00,i 会在十点自动重启( interval是一天 在这个interval day我想在它关闭的那一天之后打开一个礼物)
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();
}
}
如果您想安排每周六重置定时器,您可以使用 Android 的 WorkManager。 它支持每个 API 级别,因此您应该不会有任何问题。 这是文档:https://developer.android.com/topic/libraries/architecture/workmanager
归结为:
导入这些依赖项
implementation "androidx.work:work-runtime:2.4.0"
定义你想做什么:
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
}
}
然后每 7 天安排一次作业(从星期六开始):
PeriodicWorkRequest mWork = new PeriodicWorkRequest.Builder(TimerResetWorker.class, 7, TimeUnit.DAYS).build();
WorkManager.getInstance().enqueue(mWork);