问题描述
基本上,我需要一个暂停(基于只需几秒钟),拟投入一个动作,这样用户就可以看到下一个采取行动之前,会发生什么情况。因此,对于大酒杯,当它的经销商的转弯,他决定打,他打,卡添加,然后他决定下一步该怎么做。所以在他决定对下一步该怎么做,我想code暂停所以也可以看到至于什么庄家在做这样的经销商没有完成他的行动,在不到一秒钟,玩家只看到了结果。
Basically I need a pause (based on just a few seconds) to be put into one action so that the user can see what happens before the next action is taken. So for blackjack, when it's the dealer's turn and he decides to hit, he hits, a card is added, and then he decides what to do next. So before he decides on what to do next, I want the code to pause so it can be "seen" as to what the dealer is doing this way the dealer doesn't complete his actions in less than a second and the player only sees the results.
在此先感谢!
我要指出我一直使用的等待(此处插入的数目)尝试;但我对日食,它会导致堆栈拦截错误的排序或东西,并抛出一个异常告诉记者,这样做没有什么:(
I should note I have tried using wait(insert number here); but i am told by eclipse that it causes a stack interception error or something of the sort and throws an exception, thus doing nothing : (
嗯,这是有趣的,(我已经程序性的东西的方式是有趣,至少可以说)我做了视频下载(5000),并把它一试之下抓,它睡5秒然后继续做code。但是我更新的观点不显示后我才preSS按钮(难道真的恨事件驱动编程)。
Well this is interesting, (the way I've programed the things is "interesting" to say the least) I did the Thread.sleep(5000) and threw it under a try catch, it does sleep for 5 seconds and then continues doing the code. However my updates to views don't show until after I press a button(Is really hating event driven programming).
推荐答案
学习思考中的事件而言确实是这里的关键。你能行的。 :)
Learning to think in terms of events is indeed the key here. You can do it. :)
第一条规则就是:永远不要搪塞UI线程。在UI线程负责保持你的应用程序响应的感觉。任何工作你不应该阻止;做你需要做的,尽快恢复越好。绝对避免做I / O在UI线程上。如果您(有一些地方,你不能真正帮助它由于生命周期的要求,例如保存应用程序的状态的onPause
)曾经叫视频下载
在UI线程上,你做错了。
The first rule is: never stall the UI thread. The UI thread is responsible for keeping your app feeling responsive. Any work you do there should not block; do what you need to do and return as quickly as possible. Definitely avoid doing I/O on the UI thread. (There are some places where you can't really help it due to lifecycle requirements, for example saving app state in onPause
.) If you ever call Thread.sleep
on the UI thread you are doing it wrong.
Android的强制执行此与应用程序无响应(或ANR),错误的用户看到。每当你看到这个Android应用程序这意味着开发人员做了一些导致UI线程拖延太久。如果该设备确实陷入出于某种原因这个错误可能不是实际上是应用程序开发者的过错,但通常这意味着应用程序是做错了什么。
Android enforces this with the "Application not responding" (or "ANR") error that the user sees. Whenever you see this in an Android app it means the developer did something that caused the UI thread to stall for too long. If the device is really bogged down for some reason this error might not actually be the app developer's fault, but usually it means the app is doing something wrong.
您可以通过发布自己的事件使用这个模型,你的优势。这给你一个简单的方法告诉您的应用程序,后来做到这一点。在Android中的关键,发布自己的事件是在 处理程序
类。该方法<$c$c>postDelayed$c$c>让您安排一个 的Runnable
的意志一定数量毫秒之后执行。
You can use this model to your advantage by posting your own events. This gives you an easy way to tell your app, "do this later." In Android the key to posting your own events is in the Handler
class. The method postDelayed
lets you schedule a Runnable
that will be executed after a certain number of milliseconds.
如果你有一个活动,看起来是这样的:
If you have an Activity that looks something like this:
public class MyActivity extends Activity {
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler.postDelayed(new Runnable() {
public void run() {
doStuff();
}
}, 5000);
}
private void doStuff() {
Toast.makeText(this, "Delayed Toast!", Toast.LENGTH_SHORT).show();
}
}
在创建活动5秒钟后
然后你会看到 doStuff
。
Then 5 seconds after the activity is created you will see the toast created in doStuff
.
如果你正在写一个自定义的查看
那就更简单了。观点都有其自己的<$c$c>postDelayed$c$c>方法,将得到一切贴到正确的处理程序
键,你不需要创建自己的。
If you're writing a custom View
it's even easier. Views have their own postDelayed
method that will get everything posted to the correct Handler
and you don't need to create your own.
第二条规则是:浏览次数应仅修改的UI线程。你要和忽略的意思是这些例外了问题,如果你忽略他们你的应用程序可能会开始行为不端以有趣的方式。如果你的应用程序做大部分工作在其他线程可以<$c$c>post$c$c>直接事件,要修改,这样的修改将正常运行的观点。
The second rule is: Views should only be modified on the UI thread. Those exceptions you're getting and ignoring mean something went wrong and if you ignore them your app will probably start misbehaving in interesting ways. If your app does most of its work in other threads you can post
events directly to the view you want to modify so that the modifications will run correctly.
如果您有关于你活动
的那部分你的code,你也可以使用<$c$c>Activity#runOnUIThread$c$c>,这不正是顾名思义。你可能preFER这种方法,如果发布到一个单一的视图并没有真正意义的上下文中。
If you have a reference to your Activity
from that part of your code you can also use Activity#runOnUIThread
, which does exactly what the name implies. You might prefer this approach if posting to a single view doesn't really make sense in context.
至于更新的观点没有出现,直到你按下按钮,有什么样的看法是这些?他们是自定义的绘制这些更新的意见?如果是这样,你记住调用<$c$c>invalidate$c$c>数据更改后触发重绘?浏览次数只有重绘自己,他们已经无效后。
As for updates to views not appearing until you hit a button, what kind of views are these? Are they custom views that are drawing these updates? If so, are you remembering to call invalidate
after data changes to trigger the redraw? Views only redraw themselves after they have been invalidated.
这篇关于你怎么有code暂停在Android的几秒钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!