Android postFor循环内的延迟处理程序? [英] Android postDelayed Handler Inside a For Loop?

查看:659
本文介绍了Android postFor循环内的延迟处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在循环中运行处理程序? 我有以下代码,但无法正常工作,因为它不等待循环,而是以正确的方式执行代码:

Is there any way of running a handler inside a loop? I have this code but is not working as it does not wait for the loop but executes the code right way:

final Handler handler = new Handler();


        final Runnable runnable = new Runnable() {
            public void run() {

                // need to do tasks on the UI thread
                Log.d(TAG, "runn test");

                //
                for (int i = 1; i < 6; i++) {

                    handler.postDelayed(this, 5000);

                }


            }
        };

        // trigger first time
        handler.postDelayed(runnable, 0);

当然,当我将延迟移动的帖子移出循环之外时,它可以工作,但它不会迭代也不执行我需要的时间:

Of course when I move the post delayed outside the loop works but it does not iterate nor execute the times I need:

final Handler handler = new Handler();


        final Runnable runnable = new Runnable() {
            public void run() {

                // need to do tasks on the UI thread
                Log.d(TAG, "runn test");

                //
                for (int i = 1; i < 6; i++) {
                }

                // works great! but it does not do what we need
                handler.postDelayed(this, 5000);


            }
        };

        // trigger first time
        handler.postDelayed(runnable, 0);

解决方案:

我需要在doInBackground方法中与Thread.sleep(5000)一起使用asyntask:

I need to use asyntask along with Thread.sleep(5000) in the doInBackground method:

class ExecuteAsyncTask extends AsyncTask<Object, Void, String> {


            //
            protected String doInBackground(Object... task_idx) {

                //
                String param = (String) task_idx[0];

                //
                Log.d(TAG, "xxx - iter value started task idx: " + param);

                // stop
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //
                Log.d(TAG, "xxx - iter value done " + param);
                return " done for task idx: " + param;
            }


            //
            protected void onPostExecute(String result) {
                Log.d(TAG, "xxx - task executed update ui controls: " + result);
            }

        }




        for(int i = 0; i < 6; i ++){

            //
            new ExecuteAsyncTask().execute( String.valueOf(i) );

        }

推荐答案

您可以使Runnable实例自身调用特定的次数,而不是使用for循环.这些调用将被发布到UI线程队列中,因此请记住这一点.另外,由于延迟很大,因此请确保下次触发该事件时仍然需要该事件.

Instead of using a for loop, you can let the Runnable instance call itself for a specific number of times. These calls will be posted to UI thread queue so, keep that in mind. Also, since the delay is quite large, make sure the event is still needed when you trigger it next time.

以下代码应执行此操作:

The following code should do it:

final Handler handler = new Handler(); 
int count = 0;

final Runnable runnable = new Runnable() {
    public void run() { 
        // need to do tasks on the UI thread 
        Log.d(TAG, "Run test count: " + count);
        if (count++ < 5) {
            handler.postDelayed(this, 5000);
        }
    } 
}; 

// trigger first time 
handler.post(runnable);

这篇关于Android postFor循环内的延迟处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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