简体   繁体   English

等待 AsyncTask 完成

[英]wait for AsyncTask to finish

In the OnCreate method, I have invoked 3 AsyncTask which basically fills data for the 3 Spinners.在 OnCreate 方法中,我调用了 3 个 AsyncTask,它基本上为 3 个 Spinner 填充数据。 What I need is that I should have the Login button disabled till all 3 tasks finish.我需要的是我应该禁用登录按钮,直到所有 3 个任务完成。 How can I achieve that ?我怎样才能做到这一点?

            new SpinnerDataFetcher("GetFreeDrivers1",(Spinner)findViewById(R.id.Spinner_1)).execute();

            new SpinnerDataFetcher("GetFreeDrivers2",(Spinner)findViewById(R.id.Spinner_2)).execute();

            new SpinnerDataFetcher("GetFreeDrivers3",(Spinner)findViewById(R.id.Spinner_3)).execute();

For that you have to Disable Button before calling new Spinner1DateFetcher and call Second from Spinner1DateFetcher method onPostExecute and same as Third Spinner method and in Third Spinner onPostExecute set Button to Enable ..为此,您必须在调用new Spinner1DateFetcher之前禁用Button并从Spinner1DateFetcher方法onPostExecute调用Second并且与Third Spinner方法相同,并且在Third Spinner onPostExecute中将Button设置为Enable ..

For Disable Button use对于禁用按钮使用

Button.setEnabled(false); 

and For Enable Button use启用按钮使用

   Button.setEnabled(true); 

Edit :编辑 :

for the parameter check you have to add Constuctor and check the condition like below way.对于参数检查,您必须添加Constuctor函数并检查如下方式的条件。

private class MyAsyncTask extends AsyncTask<Void, Void, Void> {

    public MyAsyncTask(boolean showLoading) {
        super();
        // do stuff
    }

    // doInBackground() et al.
}

Initialize your AsyncTask instance with a reference to the Activity/Fragment that creates it.使用对创建它的 Activity/Fragment 的引用来初始化您的 AsyncTask 实例。 Then signal back in onPostExecute when its done然后在完成后在 onPostExecute 发出信号

eg例如

 class Spinner1DataFetcher extends AsyncTask<...> {
       public Spinner1DataFetch(YourActivityOrFragment activity) {
            _activity = activity;
       }

       protected void onPostExecute(...) {
           _activity.spinner_1_is_done();
      }
 }

Just increment a number that corresponds on how many AsyncTask are done.只需增加一个与完成多少AsyncTask相对应的数字。

        int s = 0;

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            new SpinnerDataFetcher(){
               @Override
                protected void onPostExecute(....) {
                   super.onPostExecute(...);
                   s++;
                   check();
                }

            }.execute(); 

            new SpinnerDataFetcher(){
               @Override
                protected void onPostExecute(....) {
                   super.onPostExecute(...);
                   s++;
                   check();
                }

            }.execute(); 

            new SpinnerDataFetcher(){
               @Override
                protected void onPostExecute(....) {
                   super.onPostExecute(...);
                   s++;
                   check();
                }

            }.execute(); 

        }

        public void check(){
          if(s >=3){
             s= 0;
            // enable button here
          }
        }

There are multiple ways how you can achieve this.有多种方法可以实现这一点。

The straightforward way to implement this is create a counter which will trigger UI update.实现这一点的直接方法是创建一个将触发 UI 更新的计数器。

final InterfaceTrigger trigger = new InterfaceTrigger();

new AsyncTask<>() {
    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        trigger.finishJob();
        if (trigger.isTimeToUpdateUi()) {
           // TODO update your UI
        }
    }
    };

public class InterfaceTrigger {
    private static final int THRESHOLD = 3;
    private int counter;

    public synchronized void finishJob() {
        counter++;
    }

    public synchronized boolean isTimeToUpdateUi() {
        return counter == THRESHOLD;
    }
}

Another way is to use CyclicBarier and ExcutorService mechanism.另一种方法是使用CyclicBarierExcutorService机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM