Android For 循环中的 postDelayed 处理程序?

Android postDelayed Handler Inside a For Loop?

有什么方法可以在循环中 运行 处理程序吗? 我有这段代码,但没有工作,因为它不等待循环,而是以正确的方式执行代码:

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

当然,当我将 post 延迟移动到循环外时,它可以正常工作,但它不会迭代也不会执行我需要的时间:

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 方法中使用 asyntask 和 Thread.sleep(5000):

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 线程队列,因此请记住这一点。另外,由于延迟比较大,请确保下次触发时仍然需要该事件。

下面的代码应该可以做到:

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

如果有人遇到类似问题,我对这个问题的解决方案:

int count = 0;
    public static void method(param1, param2, param3) {
                        Runnable r = () -> { //put method inside runnable
                        View view = listView.getChildAt(position); //action to be complete
                        if (view != null) { //if action is successfully complete
                            view.setSelected(true); //do something with this 
                        } else { //do a looper
                            if (count < 10) { //limited looper to certain number
                                count++;
                                method(param1, param2, param3); //run the method again
                        }
                };

                Handler h = new Handler(); //create a new Handler and post above thread with it
                h.postDelayed(r, 300);
             }

基本上,我创建了一个 if-else 语句,其中 else 语句再次运行与 postDelayed() 相同的方法进行有限次数的试验。

这可以是另一种解决方案

final Handler handler = new Handler();
Runnable runnable = new Runnable() {
    int i;
    public void run() {
        for (i = 1; i < 6; i++) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // need to do tasks on the UI thread
                    Log.d(TAG, "runn test");
                }
            }, 0);
            //Add some downtime
            SystemClock.sleep(5000);
        }
    }
};
new Thread(runnable).start();

这是我做的一个简单的逻辑,没有移动runnable里面的for loop

 for(int i = 1; i<=5; i++){
    ...
    new Handler().postDelayed(() -> myFunctionToExecute() , i * 1000);
 }

因此,每当循环迭代时,它只会延长处理程序延迟。这样,您可能会实现。我正在搜索类似的东西,找不到任何东西,因为在我的例子中我已经实现了 for 循环,将它移动到 运行() 中会造成混乱