如何模拟延迟? [英] How to simulate a delay?

查看:255
本文介绍了如何模拟延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的Android应用程序执行自动演示,因此在用户单击自动演示按钮后,它将切换到视图并延迟一秒钟,然后单击该视图上的按钮,然后2秒钟后单击在该屏幕上的另一个按钮上..依此类推,我的Java代码如下所示:

I want my Android app to do an auto demo, so after user clicks on a "Auto Demo" button, it will switch to a view and delay a second and click on a button on that view, then 2 seconds later click on another button on that screen .. so on, my java code looks like this :

  private class AutoDemoListener implements View.OnClickListener
  {
    public void onClick(View v)
    {
      Is_AutoDemo_B=true;
      Out("AutoDemoListener");
      switchView(demoView, registrationView);
      startRegistration();

      Thread t = new Thread(new Runnable()
      {
        @Override
        public void run()
        {
          runOnUiThread(new Runnable()
          {
            @Override
            public void run()
            {
              try
              {
                registrationView.symbolButton[2][8].performClick();
                Thread.sleep(1000);
                registrationView.symbolButton[4][13].performClick();
                Thread.sleep(2000);
                registrationView.symbolButton[0][1].performClick();
                Thread.sleep(1000);
                registrationView.symbolButton[6][18].performClick();
              }
              catch (InterruptedException e) { e.printStackTrace(); }
            }
          });
        }
      });
      t.start();
      Is_AutoDemo_B=false;
    }
  }

但是现在要做的是:等待4秒钟,然后一次模拟所有4次点击,因此每次点击之间没有延迟,什么是正确的方法?

But what it does now is : wait 4 seconds and simulate all 4 clicks at once, so there is no delay between each click, what's the right way to do it ?

推荐答案

您必须在后台执行延迟,并每次将结果发布回UI。

You have to perform the delay in the background and post the results back to the UI each time.

您可以使用Handler来执行此操作。 UI线程已经带有一个准备好的Looper,使您可以轻松使用Handler(其他线程则不需要,并且需要更多设置)。

You can do this using a Handler. The UI thread already comes with a prepared Looper that will allow you to easily use the Handler (other threads do not and require more setup).

可运行对象的嵌套看起来讨厌,所以这里只是增加了更多的延迟:

The nesting of runnables would look nasty, so here it is with just adding increasing delays:

private class AutoDemoListener implements View.OnClickListener
  {
    public void onClick(View v)
    {
      Is_AutoDemo_B=true;
      Out("AutoDemoListener");
      switchView(demoView, registrationView);
      startRegistration();
      final Handler handler = new Handler();

      registrationView.symbolButton[2][8].performClick();

      handler.postDelayed(new Runnable() {
          public void run() {
            registrationView.symbolButton[4][13].performClick();
          }
        }, 1000);

      handler.postDelayed(new Runnable() {
          public void run() {
            registrationView.symbolButton[0][1].performClick();
          }
        }, 3000);

      handler.postDelayed(new Runnable() {
          public void run() {
            registrationView.symbolButton[6][18].performClick();
          }
        }, 5000);

      handler.postDelayed(new Runnable() {
          public void run() {
            Is_AutoDemo_B=false;
          }
        }, 5100);

    }
  }

在科特林,这可能会更清洁使用协程:

In Kotlin this could be much cleaner using a coroutine:

val autoDemoListener = View.OnClickListener {
        Is_AutoDemo_B = true
        Out("AutoDemoListener")
        switchView(demoView, registrationView)
        startRegistration()
        CoroutineScope(Job() + Dispatchers.Main).launch {
            registrationView.symbolButton[2][8].performClick()
            delay(1000)
            registrationView.symbolButton[4][13].performClick()
            delay(2000)
            registrationView.symbolButton[0][1].performClick()
            delay(1000)
            registrationView.symbolButton[6][18].performClick()
            Is_AutoDemo_B=false
        }
    }

这篇关于如何模拟延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆