Android AsyncTask等待其他功能完成

时间:2020-08-27 11:13:02

标签: android asynchronous android-asynctask

我尝试在android中异步执行两个函数。所以我需要第一个功能放弃第二个功能。我尝试使用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...***************");

    }
}

我有两个函数,第一个是getSoldeConge();,它通过翻新来获取一些数据,第二个函数将在第二个Log.i("we are here", "add conge()");有鳍时被排除,但是在我的情况下,第二个函数将在第一个函数完成之前运行

1 个答案:

答案 0 :(得分:1)

改造调用已经在后台线程中完成,因此您无需从NBODIES进行调用。我们需要查看Retrofit代码以进一步帮助您。

顺便说一句,您不应该在2020年使用Asynctask。如果您要坚持使用Java,请使用RXJava。或AsyncTask

相关问题