什么是的setTimeout()JavaScript来Android的等价?是的、setTimeout、Android、JavaScript

由网友(我给故事你给酒)分享简介:我需要的setTimeout相当于code(通话功能(),毫秒); Android版。的setTimeout(通话功能(),毫秒);解决方案 您可能想看看的 TimerTask的 既然你提出了这个,我再次想做出不同的建议,这是一个的处理程序。它更容易使用比的TimerTask,你将不再需要明确地调用runOnUiTh...

我需要的setTimeout相当于code(通话功能(),毫秒); Android版

 的setTimeout(通话功能(),毫秒);
 

解决方案

您可能想看看的 TimerTask的

既然你提出了这个,我再次想做出不同的建议,这是一个的处理程序。它更容易使用比的TimerTask,你将不再需要明确地调用runOnUiThread的处理程序将与UI线程,只要它在UI线程创建或创建使用它的构造主要尺蠖它关联。它的工作是这样的:

 专用处理器mHandler;
可运行MyTask的=新的Runnable(){
  @覆盖
  公共无效的run(){
     //做工作
     mHandler.postDelayed(这一点,1000);
  }
}

@覆盖
公共无效的onCreate(包savedState){
  super.onCreate(savedState);
  mHandler =新的处理程序(Looper.getMainLooper());
}
//只是作为一个例子,我们将在活动开始启动任务
@覆盖
公共无效的OnStart(){
  super.onStart();
  mHandler.postDelayed(MyTask的,1000);
}

//在你的程序有些时候你可能会想处理程序停止(在的onStop是个好地方)
@覆盖
公共无效的onStop(){
  super.onStop();
  mHandler.removeCallbacks(MyTask的);
}
 
几张图告诉你事件循环和Job Queue究竟是什么

有一些事情要注意,在你的活动处理程序:

您的活动可以关机/不可见的,而你的处理程序仍在运行,如果你(如果你在onResume启动,或在onPause)不停止它的onStop,这会造成问题,如果你尝试更新UI 如果您的手机进入深度睡眠的处理程序将无法正常发出您指定的次数。我知道这是我做了一些大量的测试与蓝牙设备多小时手术后,来测试连接,我曾与打印日志在每次发射时使用的处理器一起。 如果您需要此计时器是持续的,我建议把它在这将持续超过一个活动的服务。注册您的活动的服务(通过实现与服务通信服务定义的接口)。

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

setTimeOut(call function(),milliseconds);

解决方案

You probably want to check out 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:

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:

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

阅读全文

相关推荐

最新文章