我想调用多个AsynTask
。它没有用,所以我用谷歌搜索寻求帮助。我发现如果我使用 AsyncTask.THREAD_POOL_EXECUTOR
那么它就会工作。
但它不起作用。
我这样调用它:
new GetCategory().execute();
new GetArea().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
请帮我解决这个问题。我想一次调用它,或者在 GetCategory()
之后调用 GetArea()
也可以。
我没有收到错误。应用程序可能正在做太多工作。
日志:
03-13 21:21:43.134: I/Choreographer(1222): Skipped 39 frames! The application may be doing too much work on its main thread.
03-13 21:21:43.394: I/Choreographer(1222): Skipped 43 frames! The application may be doing too much work on its main thread.
03-13 21:21:43.634: I/Choreographer(1222): Skipped 36 frames! The application may be doing too much work on its main thread.
03-13 21:21:43.784: I/Choreographer(1222): Skipped 38 frames! The application may be doing too much work on its main thread.
03-13 21:21:44.004: I/Choreographer(1222): Skipped 56 frames! The application may be doing too much work on its main thread.
03-13 21:21:44.164: I/Choreographer(1222): Skipped 36 frames! The application may be doing too much work on its main thread.
03-13 21:21:45.495: I/Choreographer(1222): Skipped 33 frames! The application may be doing too much work on its main thread.
03-13 21:21:45.765: I/Choreographer(1222): Skipped 43 frames! The application may be doing too much work on its main thread.
03-13 21:21:46.005: I/Choreographer(1222): Skipped 36 frames! The application may be doing too much work on its main thread.
03-13 21:21:46.235: I/Choreographer(1222): Skipped 44 frames! The application may be doing too much work on its main thread.
03-13 21:21:46.525: I/Choreographer(1222): Skipped 48 frames! The application may be doing too much work on its main thread.
03-13 21:21:46.855: I/Choreographer(1222): Skipped 39 frames! The application may be doing too much work on its main thread.
03-13 21:21:47.005: I/Choreographer(1222): Skipped 39 frames! The application may be doing too much work on its main thread.
03-13 21:21:47.156: I/Choreographer(1222): Skipped 37 frames! The application may be doing too much work on its main thread.
03-13 21:21:47.306: I/Choreographer(1222): Skipped 38 frames! The application may be doing too much work on its main thread.
我在两个 AsynTask
上都绑定(bind)了 Spinner。
我的代码:
Area[] areaList = gson.fromJson(result, Area[].class);
List<Area> lList = Arrays.asList(areaList);
Area objArea;
areaId.add("0");
areaName.add("Select Area");
for (int i = 0; i < lList.size(); i++) {
objArea = lList.get(i);
areaName.add(objArea.getAreaName());
areaId.add(objArea.getAreaId());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_dropdown_item_1line, areaName);
((Spinner) spnCEArea).setAdapter(adapter);
最佳答案
之所以不能在同一个项目中的AsyncTask
上同时调用两个.execute()
方法是因为Android自引入的修改> Gingerbread
。在此版本之前,您可以根据需要多次调用 .execute()
,因为它们中的每一个都在单独的 Thread
中运行。
但是从这个版本开始,它们是按顺序调用的,所以如果一个正在执行,如果您只调用 .execute()
,另一个将不会启动。
所以基本上您需要做的就是检查您正在运行 AsyncTask
的设备的版本,并根据它运行一个或其他命令。
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR1)
your_asynctask.execute(your_params);
else
your_asynctask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, your_params);
更多信息 here .
顺便说一句,The application may be doing too much work on its main thread.
行与 AsyncTask
无关。这些任务在后台运行,如果您在 doInBackground()
方法中完成这些工作,那不是问题。
如果您在 onPostExecute()
任务中做一些工作,情况可能会略有不同,因为这可能与您的主 UI 有关,但我们不能在不知道的情况下告诉您更多信息您正在运行的代码。
关于android - 一次调用多个 AsyncTask 在 Android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22383604/