如何使用计数器在特定日期结束并在另一天重新开始计数(倒计时)?

huangapple go评论60阅读模式
英文:

How to work a counter that ends a certain day and restarts the count on another day (CountDown)?

问题

日程表 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(), "星期天八点了", Toast.LENGTH_SHORT).show();

        if (cal.get(Calendar.MINUTE) == 19) {

            Toast.makeText(getApplicationContext(), "星期天十九分了", Toast.LENGTH_SHORT).show();

            resetTimer();

        } else {
            Toast.makeText(getApplicationContext(), "不是十九分", Toast.LENGTH_SHORT).show();
        }

    } else {
        Toast.makeText(getApplicationContext(), "不是八点", Toast.LENGTH_SHORT).show();
    }

} else {
    Toast.makeText(getApplicationContext(), "不是星期天", Toast.LENGTH_SHORT).show();
}
英文:

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();


    }


}

答案1

得分: 0

如果您想要在每个星期六定时重置计时器,您可以使用 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(); // 进行您想要的操作
     return Result.success(); // 如果出现问题,您还可以返回 Result.failure() / retry()
   }
}

然后,只需安排每7天执行一次的任务(从星期六开始):

PeriodicWorkRequest mWork = new PeriodicWorkRequest.Builder(TimerResetWorker.class, 7, TimeUnit.DAYS).build();
WorkManager.getInstance().enqueue(mWork);
英文:

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);

huangapple
  • 本文由 发表于 2020年8月16日 18:09:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63435587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定