android中如何让代码暂停几秒钟?

发布于 2024-09-30 21:06:34 字数 404 浏览 6 评论 0原文

基本上,我需要在一个操作中暂停(仅几秒钟),以便用户可以看到在采取下一个操作之前会发生什么。因此,对于二十一点来说,当轮到庄家并且他决定要牌时,他就牌,然后添加一张牌,然后他决定下一步要做什么。因此,在他决定下一步要做什么之前,我希望代码暂停,以便可以“看到”庄家正在做什么,这样庄家不会在不到一秒的时间内完成他的操作,而玩家只能看到结果。

提前致谢!

我应该注意我已经尝试使用 wait(在此处插入数字);但 Eclipse 告诉我,它会导致堆栈拦截错误或类似的错误并引发异常,因此什么也不做:(

这很有趣,(我编程的方式至少可以说是“有趣”的) )我做了 Thread.sleep(5000) 并将其放在 try catch 下,它确实休眠了 5 秒,然后继续执行代码,但是我对视图的更新直到我按下按钮后才显示(真的很讨厌)。事件驱动编程)。

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.

Thanks in advance!

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 : (

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

电影里的梦 2024-10-07 21:06:34

学会从事件的角度思考确实是这里的关键。你可以做到的。 :)

第一条规则是:永远不要停止 UI 线程。 UI 线程负责让您的应用程序保持响应。您在那里所做的任何工作都不应该受到阻碍;做你需要做的事情并尽快返回。绝对避免在 UI 线程上执行 I/O。 (由于生命周期要求,有些地方您无能为力,例如在 onPause 中保存应用程序状态。)如果您曾经调用 Thread.在 UI 线程上 sleep 你做错了。

Android 通过用户看到的“应用程序未响应”(或“ANR”)错误强制执行此操作。每当您在 Android 应用程序中看到此内容时,就意味着开发人员执行了某些操作,导致 UI 线程停滞太长时间。如果设备确实由于某种原因陷入困境,这个错误实际上可能不是应用程序开发人员的错,但通常意味着应用程序做错了什么。

您可以通过发布您自己的事件来利用此模型。这为您提供了一种简单的方法来告诉您的应用程序“稍后再执行此操作”。在 Android 中,发布自己的事件的关键是在 Handler 类。方法 postDelayed< /code>允许您安排 Runnable 将在一定毫秒数后执行。

如果您的 Activity 看起来像这样:

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();
    }
}

那么在创建该 Activity 5 秒后,您将看到在 doStuff 中创建的 toast。

如果您正在编写自定义View,那就更容易了。视图有自己的 postDelayed 方法会将所有内容发布到正确的Handler,并且您无需创建自己的。

第二条规则是:视图应该在 UI 线程上修改。您收到并忽略的那些异常意味着出现了问题,如果您忽略它们,您的应用程序可能会开始以有趣的方式出现错误行为。如果您的应用程序在其他线程中完成大部分工作,您可以 post 事件直接到您要修改的视图,以便修改正确运行。

如果您从代码的该部分引用了 Activity,您还可以使用 Activity#runOnUIThread,其功能正如其名称所暗示的那样。如果发布到单个视图在上下文中没有真正意义,您可能更喜欢这种方法。

至于在按下按钮之前不会出现的视图更新,这些是什么类型的视图?它们是绘制这些更新的自定义视图吗?如果是这样,您是否记得调用 invalidate 数据改变后触发重绘?视图只会在失效后重新绘制自己。

Learning to think in terms of events is indeed the key here. You can do it. :)

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

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();
    }
}

Then 5 seconds after the activity is created you will see the toast created in doStuff.

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.

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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文