等待时间 - 机器人 [英] Wait a time - Android

查看:97
本文介绍了等待时间 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要我的程序等待我的,而10S真实的,但它不工作 我试着用了Thread.sleep(10000);但它不是10秒

I want that my program wait 10s in my while true, but it doesn't work I tried to use Thread.sleep(10000); but it isn't 10s

while (true) {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 5; j++) {
                if (matrixVacancy[i][j] == 1) {
                    completeParking(i, j, R.color.vaga_ocupada);
                } else {
                    completeParking(i, j, R.color.cor_chao);
                }
            }
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException ex) {
        }

        int a, b, c, d, e, f, g, h, i;

        a = (int) (Math.random() * 2); // indice i
        b = (int) (Math.random() * 5); // indice j
        c = (int) (Math.random() * 2); // tem ou nao carro

        d = (int) (Math.random() * 2); // indice i
        e = (int) (Math.random() * 5); // indice j
        f = (int) (Math.random() * 2); // tem ou nao carro

        g = (int) (Math.random() * 2); // indice i
        h = (int) (Math.random() * 5); // indice j
        i = (int) (Math.random() * 2); // tem ou nao carro

        matrixVacancy[a][b] = c;
        matrixVacancy[d][e] = f;
        matrixVacancy[g][h] = i;
    }

我该怎么办呢?对于我而等待10秒?

How can I do it? For my while wait 10s?

推荐答案

取决于什么线程你想睡觉。你也可以把你的方法,在一个单独的线程,做你的方法存在。这样,你的应用程序将不会挂起/睡眠

Depends what thread your trying to sleep. You can also put your method in a seperate thread and do your methods there. This way your app will not hang/sleep

private class TimeoutOperation extends AsyncTask<String, Void, Void>{

    @Override
    protected Void doInBackground(String... params) {

        try {
            Log.i(TAG, "Going to sleep");
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Log.i(TAG, "This is executed after 10 seconds and runs on the main thread");
        //Update your layout here
        super.onPostExecute(result);
    }
}

要执行此操作使用

new TimeoutOperation().execute("");

这篇关于等待时间 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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