如何解决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()");
有鳍时被排除,但是在我的情况下,第二个函数将在第一个函数完成之前运行
解决方法
改造调用已经在后台线程中完成,因此您无需从NBODIES
进行调用。我们需要查看Retrofit代码以进一步帮助您。
顺便说一句,您不应该在2020年使用Asynctask
。如果您要坚持使用Java,请使用RXJava。或AsyncTask
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。