如何延缓执行的android [英] How to delay execution android

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

问题描述

我建立一个机器人棋盘游戏为特色的AI。人工智能得到了转弯,必须调用一系列后,它的帖子无效到我的自定义视图更新的动作。

I am building an android board game which features AI. The AI gets a turn and has to invoke a series of actions after which it posts invalidate to my custom view to update.

我需要让用户获取看到AI有轮到它,而不是它通过闪烁来减缓这些行动。

I need to slow down these actions so the user gets to see the AI having its turn rather than it flashing by.

我已经试过这些方针的东西

I have tried something along these lines

    try {
        doFirstThing();
        Thread.sleep(500)
        //post invalidate

        doNextThing();
        Thread.sleep(1000)
        //post invalidate
     }
     catch (Exception e) {
     }

然而,这是具有绝对没有影响。也这是在一个单独的线程中运行,如果这不明显。

However this is having absolutely no effect. Also this is running in a separate thread if this wasn't obvious.

请告诉我我最好的选择我看着处理,但他们并不需要正确的,因为我需要执行一系列任务的顺序,每次更新视图。

Whats my best option I've looked at handler but they don't need right as i need to execute a series of tasks in sequence updating the view each time.

推荐答案

使用处理程序,这是一个好主意,如果你是从UI线程执行...

Using a Handler, which is a good idea if you are executing from a UI thread...

    final Handler h = new Handler();

    final Runnable r2 = new Runnable() {

        @Override
        public void run() {
            // do second thing
        }
    };

    Runnable r1 = new Runnable() {

        @Override
        public void run() {
            // do first thing
            h.postDelayed(r2, 10000); // 10 second delay
        }
    };

    h.postDelayed(r1, 5000); // 5 second delay

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

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