如何使Android程序“等待" [英] How to make an Android program 'wait'

查看:231
本文介绍了如何使Android程序“等待"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让程序暂停一定的毫秒数,我该怎么做呢?

I want to cause my program to pause for a certain number of milliseconds, how exactly would I do this?

我发现了不同的方式,例如Thread.sleep( time ),但是我认为这不是我所需要的.我只想让我的代码在某行上暂停x毫秒.任何想法将不胜感激.

I have found different ways such as Thread.sleep( time ), but I don't think that is what I need. I just want to have my code pause at a certain line for x milliseconds. Any ideas would be greatly appreciated.

这是C语言中的原始代码...

This is the original code in C...

extern void delay(UInt32 wait){
    UInt32 ticks;
    UInt32  pause;

    ticks = TimGetTicks();
    //use = ticks + (wait/4);
    pause = ticks + (wait);
    while(ticks < pause)
        ticks = TimGetTicks();
}

等待时间是毫秒

推荐答案

好的,首先,在执行操作时,切勿使用忙循环实现延迟.我可以看到它的来源-我猜想手掌飞行员是没有内置sleep()函数的单进程设备,但是在多进程设备上,像这样的繁忙循环只会带来整个过程.处理器屈膝它会耗尽您的电池电量,阻止正常的系统任务正常运行,减慢其他程序的速度或使其完全停止运行,使设备无响应,甚至使它变得发烫.

OK, first of all, never implement a delay with a busy loop as you're doing. I can see where that comes from -- I'm guessing that the palm pilot was a single-process device with no built-in sleep() function, but on a multi-process device, a busy loop like that just brings the entire processor to its knees. It kills your battery, prevents normal system tasks from running properly, slows down other programs or stops them completely, makes the device unresponsive, and can even cause it to get hot in your hands.

您要查找的呼叫是Thread.sleep().您需要进行设置以捕获睡眠时发生的任何中断异常.

The call you're looking for is Thread.sleep(). You'll need to set up to catch any interrupt exceptions that occur while you're sleeping.

第二,使用基于事件的用户界面(例如Android(或几乎任何现代GUI系统)),您永远都不想在UI线程中入睡.如其他张贴者所提到的那样,这也将冻结整个设备,并导致ANR(不响应活动)崩溃.最重要的是,这些冻结完全破坏了用户体验.

Second, with event-based user interfaces such as Android (or pretty much any modern GUI system), you never want to sleep in the UI thread. That will also freeze up the entire device and result in an ANR (Activity Not Responding) crash, as other posters have mentioned. Most importantly, those little freezes totally ruin the user experience.

(例外:如果您睡眠的时间间隔足够短,以至于用户可能不会注意到,那么您就可以摆脱它.虽然可能会根据情况使应用程序变笨,但可能需要1/4秒)

(Exception: if you're sleeping for short enough intervals that the user probably won't notice, you can get away with it. 1/4 second is probably ok, although it can make the application janky depending on the situation.)

不幸的是,如果您正在做的事情是将基于循环的应用程序移植到基于事件的系统中,则没有干净而优雅的方法可以完成您想要的事情.

Unfortunately, there's no clean and elegant way to do what you want if what you're doing is porting a loop-based application to an event-based system.

也就是说,正确的过程是在UI线程中创建一个处理程序,然后向其发送延迟的消息.延迟的消息将唤醒"您的应用程序,并触发它执行延迟后将要执行的操作.

That said, the proper procedure is to create a handler in your UI thread and send delayed messages to it. The delayed messages will "wake up" your application and trigger it to perform whatever it was going to do after the delay.

类似这样的东西:

View gameBoard;     // the view containing the main part of the game
int gameState = 0;  // starting
Handler myHandler;

public void onCreate(Bundle oldState) {
  super.onCreate(oldState);
  ...
  gameBoard = findViewById(R.layout.gameboard);
  myHandler = new Handler();
  ...
}

public void onResume() {
  super.onResume();
  displayStartingScreen();
  myHandler.postDelayed(new Runnable() { 
    gotoState1(); 
  }, 250);
}

private void gotoState1() {
  // It's now 1/4 second since onResume() was called
  displayNextScreen();
  myHandler.postDelayed(new Runnable() { 
    gotoState2();
  }, 250);
}
...

这篇关于如何使Android程序“等待"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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