如何在Android开发中使用Java设置超时?

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

How to set a Timeout in java for Android Development?

问题

我正在开发一个适用于 Android 4.0 及更高版本的应用,使用 Java 语言。

我想在 Java 中设置一个自动启动并在 5 秒后运行一个方法的超时方法。

请告诉我如何做,附带解释。

我在互联网上做了很多关于这个的研究,但是我没有得到任何结果。

英文:

I'm working on an app for Android 4.0 and later using Java.

I want to set a timeout method in java that starts automatically and runs a method after 5 seconds.

Please tell me how to do that with explanation.

I researched a lot about it on the Internet, but I couldn't get any result.

答案1

得分: 0

这将解决你的问题:

计时器 timer = new 计时器();
计时器任务 task = new 计时器任务() {
   public void 运行() {
      // 在这里放入你的方法代码
   }
};
timer.安排(task, 5000);
英文:

This will solve your problem :

Timer timer = new Timer();
TimerTask task = new TimerTask() {
   public void run() {
      // Your method code here
   }
};
timer.schedule(task, 5000);

huangapple
  • 本文由 发表于 2023年3月12日 16:37:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711936.html
匿名

发表评论

匿名网友

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

确定