提问人:holyCrap 提问时间:2/19/2023 更新时间:2/19/2023 访问量:27
AsyncTask 和普通代码之间的顺序是什么?
What is the order between AsyncTask and normal code?
问:
我添加了AsyncTask类文件以从API服务器接收Json文件。
那一刻,我想知道调用AsyncTask的代码与代码下的普通代码之间的顺序。
我知道AsyncTask是在后台执行的。
如果我对 AsyncTask 的理解是正确的,那么在 AsyncTask 代码运行时必须运行普通代码。
但我的想法是错误的。
任务返回结果后,正常代码开始运行!!
我用 postDelayed() 方法和输入 5000 毫秒对此进行了测试。
这是我的代码
try {
currencyRate = new Task().execute(fromto).get(); // This is calling my AsyncTask class
Log.e("start", "AsyncTask");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
double input = Double.parseDouble(et_from.getText().toString());
double result = Math.round(input * currencyRate * 100.0) / 100.0;
tv_to.setText(Double.toString(result));
Log.e("end", "poatDelayed for 5000 millisec");
}
}, 5000);
2 个日志时间之间的差异正好是 5000 毫秒。
我原以为时间会短于 5000 年,但事实并非如此。
这是我的logcat
2023-02-19 23:29:00.294 17115-17115/com.example.moneyexchanger E/start: AsyncTask
2023-02-19 23:29:05.303 17115-17115/com.example.moneyexchanger E/end: poatDelayed for 5000 millisec
怎么可能???
答: 暂无答案
评论
AsyncTask