setTimeOut()javascript到Android的等价物是什么?

时间:2022-05-20 20:30:45

I need the equivalent code of setTimeOut(call function(),milliseconds); for android.

我需要setTimeOut的等效代码(call function(),milliseconds);对于Android。

setTimeOut(call function(),milliseconds);

4 个解决方案

#1


21  

You probably want to check out TimerTask

您可能想查看TimerTask

Since you brought up this again I'd like to make a different recommendation, which is a Handler. It is simpler to use than a TimerTask as you won't need to explicitely call runOnUiThread as the Handler will be associated with the UI thread so long as it's created on the UI thread or you create it using the main looper in it's constructor. It would work like this:

既然你再次提出这个问题,我想提出一个不同的推荐,即Handler。它比TimerTask更简单,因为你不需要明确地调用runOnUiThread,因为Handler将与UI线程相关联,只要它在UI线程上创建或者你使用它的构造函数中的主循环器创建它。它会像这样工作:

private Handler mHandler;
Runnable myTask = new Runnable() {
  @Override
  public void run() {
     //do work
     mHandler.postDelayed(this, 1000);
  }
}

@Override
public void onCreate(Bundle savedState) {
  super.onCreate(savedState);
  mHandler = new Handler(Looper.getMainLooper());
}
//just as an example, we'll start the task when the activity is started
@Override
public void onStart() { 
  super.onStart();
  mHandler.postDelayed(myTask, 1000);
}

//at some point in your program you will probably want the handler to stop (in onStop is a good place)
@Override
public void onStop() {
  super.onStop();
  mHandler.removeCallbacks(myTask);
}

There are some things to be aware of with handlers in your activity:

您的活动中有一些需要了解处理程序的事项:

  1. Your activity can be shutdown/non visible while your handler is still running if you don't stop it in onStop (or onPause if you started it in onResume), this will lead to problems if you try to update the UI
  2. 如果你没有在onStop中停止它,你的活动可以在你的处理程序仍在运行时关闭/不可见(如果你在onResume中启动它,则会暂停),如果你尝试更新UI,这将导致问题

  3. If your phone goes into deep sleep the handlers won't fire as often as you have specified. I know this as I've done some extensive testing with bluetooth devices to test connectivity after many hours of operation and I had used handlers along with log prints every time they fired.
  4. 如果您的手机进入深度睡眠状态,处理程序将不会像您指定的那样经常发射。我知道这一点,因为我已经用蓝牙设备进行了一些广泛的测试,以便在运行数小时后测试连接性,并且每次启动时我都使用处理程序和日志打印。

  5. If you need this timer to be ongoing I recommend putting it in a service which will last longer than an activity. Register your activity with the service(by implementing an interface defined in the service for communication with the service).
  6. 如果你需要这个计时器,我建议把它放在一个持续时间比活动更长的服务中。使用服务注册您的活动(通过实现服务中定义的接口与服务进行通信)。

#2


11  

This is the code that i used in my current project. I used TimerTask as Matt said. 60000 is the milisec. = 60 sec. i used it to refresh match scores.

这是我在当前项目中使用的代码。 Matt说,我使用了TimerTask。 60000是milisec。 = 60秒我用它来刷新比赛分数。

private void refreshTimer() {
        autoUpdate = new Timer();
        autoUpdate.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        adapter = Score.getScoreListAdapter(getApplicationContext());
                        adapter.forceReload();
                        setListAdapter(adapter);
                    }
                });
            }
        }, 0, 60000);

#3


1  

As a continuation to Valentyn answer using java underscore:

作为使用java下划线的Valentyn答案的延续:

Add dependency to gradle:

向gradle添加依赖项:

dependencies {
    compile group: 'com.github.javadev', name: 'underscore', version: '1.15'
}

Java:

import com.github.underscore.lodash.$;

$.setTimeout(new Function<Void>() {
    public Void apply() {
        // work
        return null;
    }
}, 1000); // 1 second

#4


1  

There is setTimeout() method in underscore-java library. I am the maintainer of the project.

underscore-java库中有setTimeout()方法。我是该项目的维护者。

Code example:

import com.github.underscore.lodash.U;
import com.github.underscore.Function;

public class Main {

    public static void main(String[] args) {
        final Integer[] counter = new Integer[] {0};
        Function<Void> incr = new Function<Void>() { public Void apply() {
            counter[0]++; return null; } };
        U.setTimeout(incr, 100);
    }
}

The function will be started in 100ms with a new thread.

该函数将在100ms内以新线程启动。

#1


21  

You probably want to check out TimerTask

您可能想查看TimerTask

Since you brought up this again I'd like to make a different recommendation, which is a Handler. It is simpler to use than a TimerTask as you won't need to explicitely call runOnUiThread as the Handler will be associated with the UI thread so long as it's created on the UI thread or you create it using the main looper in it's constructor. It would work like this:

既然你再次提出这个问题,我想提出一个不同的推荐,即Handler。它比TimerTask更简单,因为你不需要明确地调用runOnUiThread,因为Handler将与UI线程相关联,只要它在UI线程上创建或者你使用它的构造函数中的主循环器创建它。它会像这样工作:

private Handler mHandler;
Runnable myTask = new Runnable() {
  @Override
  public void run() {
     //do work
     mHandler.postDelayed(this, 1000);
  }
}

@Override
public void onCreate(Bundle savedState) {
  super.onCreate(savedState);
  mHandler = new Handler(Looper.getMainLooper());
}
//just as an example, we'll start the task when the activity is started
@Override
public void onStart() { 
  super.onStart();
  mHandler.postDelayed(myTask, 1000);
}

//at some point in your program you will probably want the handler to stop (in onStop is a good place)
@Override
public void onStop() {
  super.onStop();
  mHandler.removeCallbacks(myTask);
}

There are some things to be aware of with handlers in your activity:

您的活动中有一些需要了解处理程序的事项:

  1. Your activity can be shutdown/non visible while your handler is still running if you don't stop it in onStop (or onPause if you started it in onResume), this will lead to problems if you try to update the UI
  2. 如果你没有在onStop中停止它,你的活动可以在你的处理程序仍在运行时关闭/不可见(如果你在onResume中启动它,则会暂停),如果你尝试更新UI,这将导致问题

  3. If your phone goes into deep sleep the handlers won't fire as often as you have specified. I know this as I've done some extensive testing with bluetooth devices to test connectivity after many hours of operation and I had used handlers along with log prints every time they fired.
  4. 如果您的手机进入深度睡眠状态,处理程序将不会像您指定的那样经常发射。我知道这一点,因为我已经用蓝牙设备进行了一些广泛的测试,以便在运行数小时后测试连接性,并且每次启动时我都使用处理程序和日志打印。

  5. If you need this timer to be ongoing I recommend putting it in a service which will last longer than an activity. Register your activity with the service(by implementing an interface defined in the service for communication with the service).
  6. 如果你需要这个计时器,我建议把它放在一个持续时间比活动更长的服务中。使用服务注册您的活动(通过实现服务中定义的接口与服务进行通信)。

#2


11  

This is the code that i used in my current project. I used TimerTask as Matt said. 60000 is the milisec. = 60 sec. i used it to refresh match scores.

这是我在当前项目中使用的代码。 Matt说,我使用了TimerTask。 60000是milisec。 = 60秒我用它来刷新比赛分数。

private void refreshTimer() {
        autoUpdate = new Timer();
        autoUpdate.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        adapter = Score.getScoreListAdapter(getApplicationContext());
                        adapter.forceReload();
                        setListAdapter(adapter);
                    }
                });
            }
        }, 0, 60000);

#3


1  

As a continuation to Valentyn answer using java underscore:

作为使用java下划线的Valentyn答案的延续:

Add dependency to gradle:

向gradle添加依赖项:

dependencies {
    compile group: 'com.github.javadev', name: 'underscore', version: '1.15'
}

Java:

import com.github.underscore.lodash.$;

$.setTimeout(new Function<Void>() {
    public Void apply() {
        // work
        return null;
    }
}, 1000); // 1 second

#4


1  

There is setTimeout() method in underscore-java library. I am the maintainer of the project.

underscore-java库中有setTimeout()方法。我是该项目的维护者。

Code example:

import com.github.underscore.lodash.U;
import com.github.underscore.Function;

public class Main {

    public static void main(String[] args) {
        final Integer[] counter = new Integer[] {0};
        Function<Void> incr = new Function<Void>() { public Void apply() {
            counter[0]++; return null; } };
        U.setTimeout(incr, 100);
    }
}

The function will be started in 100ms with a new thread.

该函数将在100ms内以新线程启动。