简体   繁体   English

Android AsyncTask 等待其他函数完成

[英]Android AsyncTask wait other function to finish

I try to execute two function asynchronously in android.我尝试在android中异步执行两个函数。 So i need that the first function waid the second function.所以我需要第一个函数等待第二个函数。 I try to use the AsyncTask to do this.But the problem is that the first function can not wait until the first function finished.我尝试使用AsyncTask来做到这一点。但问题是第一个函数不能等到第一个函数完成。

    private class DemandeConge extends AsyncTask<Void, Void, Void> {
    protected void onPreExecute() {
        super.onPreExecute();
        Log.i("we are here", "**************On pre Exceute......***************");
        p=new ProgressDialog(getActivity());
        p.setMessage("Please wait...It is downloading");
        p.setIndeterminate(false);
        p.setCancelable(false);
        p.show();
        Log.i("we are here", "**************On pre Exceute......***************");
    }

    @Override
    protected Void doInBackground(Void... params) {
        Log.i("we are here", "**************oOn doInBackground...***************");
        getSoldeConge();
        Log.i("we are here", "**************On doInBackground...***************");
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
        Log.i("we are here", "**************onProgressUpdate...***************");
        Log.i("we are here", "**************onProgressUpdate...***************");
    }

    /**
     * `onPostExecute` is run after `doInBackground`, and it's
     * run on the main/ui thread, so you it's safe to update ui
     * components from it. (this is the correct way to update ui
     * components.)
     */
    @Override
    protected void onPostExecute(Void param) {

        Log.i("we are here", "**************onPostExecute...***************");
        Log.i("we are here", "**************onPostExecute...***************");

        Log.i("we are here", "add conge()");
        p.dismiss();

    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        p.dismiss();
        Log.i("we are here", "**************onCancelled...***************");

    }
}

I have two function the first is getSoldeConge();我有两个函数,第一个是getSoldeConge(); which fetch some data with retrofit and the second will be exexuted when the second finised Log.i("we are here", "add conge()");它通过改造获取一些数据,当第二个完成Log.i("we are here", "add conge()"); but in my case the second function will run bofore the first fuction finished.但在我的情况下,第二个函数将在第一个函数完成之前运行。

Retrofit calls are already done in a background thread, so you don't need to call them from an Asynctask .改造调用已经在后台线程中完成,因此您不需要从Asynctask调用它们。 We'll need to see the Retrofit code to help you any further.我们需要查看改造代码以进一步帮助您。

By the way, you shouldn't be using AsyncTask in 2020. If you're going to stick with Java, use RXJava.顺便说一句,您不应该在 2020 年使用AsyncTask 。如果您打算坚持使用 Java,请使用 RXJava。 Or ExecutorService .ExecutorService

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

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