如何在 Kotlin 中实现时间调度器

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

How to implement a time scheduler in Kotlin

问题

以下是您要翻译的内容:

因此,我希望我的应用程序在特定的时间间隔内执行操作。经过一些研究,我在 stackoverflow 上找到了一些答案,它引导我到了这个链接,介绍了一个叫做 fixedRateTimer 的东西:以下是该页面上的第一个示例:

val fixedRateTimer = fixedRateTimer(name = "hello-timer",
initialDelay = 100, period = 100) {
    println("hello world!")
}

try {
    Thread.sleep(1000)
} finally {
    fixedRateTimer.cancel()
}

当我添加了这段代码片段后,出现了一个错误。

“无法将表达式'fixedRateTimer'视为函数调用。找不到函数'invoke()',变量'fixedRateTimer'必须初始化”

我进行了更多的研究,并导入了kotlin.concurrent.schedule*kotlin.concurrent.fixedRateTimer*,并在第一个代码块之前添加了一段代码块来进行初始化。

所以,它修复了第一个代码块,但我刚刚添加的那个代码块却给我带来了更多的错误。它指出函数需要是抽象的。所以我将它设为抽象,但是底部出现了一个错误,指出该函数需要是内联的,而不是抽象的。

所以我变得有些疯狂,因为我使用的所有代码块都来自文档,所以我不确定为什么会出现这些错误。作为一个初学者,我试图遵循文档,但在每一个转折点上我总是感到迷茫。在我链接的 Stackoverflow 页面上还有一些其他答案,比如 Handlers 和 schedules,但我仍然遇到了类似的错误。

那么,我应该如何使用 schedule 或 fixedRateTimer?我希望能有一个逐步的示例,以便我完全理解我所输入的内容。

谢谢!

英文:

So I want my app to perform an operation at certain intervals of time. After doing a little research I found the some answers on stackoverflow and it led me to this link to something called fixedRateTimer: Heres the first example in that page

    val fixedRateTimer = fixedRateTimer(name = "hello-timer",
    initialDelay = 100, period = 100) {
    println("hello world!")}

    try {
   Thread.sleep(1000)
   } 

   finally {
   fixedRateTimer.cancel();
   }

When I added that snippet of code I get an error.

"Expression 'fixedRateTimer' cannot be invoked as a function. The function 'invoke()' is not found
Variable 'fixedRateTimer' must be initialized"

I did more research and imported kotlin.concurrent.schedule* and kotlin.concurrent.fixedRateTimer and added a block of code on top of the first to initilize it.

如何在 Kotlin 中实现时间调度器

So it fixed the first block code but the one I just added gave me more errors. It stated the function needed to be abstract. So i made it abstract but then an error appeared at the bottom of crossinline stating that the function needs to be inline and not abstract.

So im going crazy because all the code blocks I took are from the documentations so im not sure why im getting these errors. As a beginner Im trying to follow the documentation but im always at a lost at every turn. There were some other answers on that Stackoverflow page I linked like Handlers and schedules but I was still getting similiar type of errors.

So how do I use schedule or fixedRateTimer ? I would appreciate a step-by-step example so I could a get fully understanding of what im typing

Thanks!

答案1

得分: 3

Option 1:
使用此链接:-

https://developer.android.com/topic/libraries/architecture/workmanager/basics

现在,由于您想要执行定期任务,您需要使用

Periodic Work 请求,而不是 OneTimeWorkRequest,您可以在这里设置间隔。

 PeriodicWorkRequest saveRequest =
   new PeriodicWorkRequest.Builder(SaveImageToFileWorker.class, 1, TimeUnit.HOURS)
       // 约束条件
       .build();

按照此示例:-

https://developer.android.com/topic/libraries/architecture/workmanager/how-to/define-work

注意:- 仅当您的应用的最小目标API为Android 5.0时,才能使用 WorkManager API。

Option 2:

   new CountDownTimer(30000, 1000) {

 public void onTick(long millisUntilFinished) {
     mTextField.setText("剩余秒数:" + millisUntilFinished / 1000);
 }

 public void onFinish() {
     mTextField.setText("完成!");
 }

}.start();

英文:

Option 1 :
use this link :-

https://developer.android.com/topic/libraries/architecture/workmanager/basics

Now, since you want to do a periodic task, you have to use,

Periodic Work request instead of OneTimeWorkRequest and you can set the interval here.

 PeriodicWorkRequest saveRequest =
   new PeriodicWorkRequest.Builder(SaveImageToFileWorker.class, 1, TimeUnit.HOURS)
       // Constraints
       .build();

Follow this example :-

https://developer.android.com/topic/libraries/architecture/workmanager/how-to/define-work

Note :-Use of WorkManager API cannly be done if minimum API that you are targetting your app is Android 5.0

option 2 :

   new CountDownTimer(30000, 1000) {

 public void onTick(long millisUntilFinished) {
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     mTextField.setText("done!");
 }

}.start();

huangapple
  • 本文由 发表于 2020年10月23日 07:31:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64491903.html
匿名

发表评论

匿名网友

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

确定