同时调用多个AsyncTask的Andr​​oid中不工作 [英] Call multiple AsyncTask at a time not working in Android

查看:641
本文介绍了同时调用多个AsyncTask的Andr​​oid中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要调用多个 AsynTask 。这不是工作,所以我谷歌的一些帮助。我发现,如果我使用 AsyncTask.THREAD_POOL_EXECUTOR 然后它会正常工作。

但它不工作。

我如下调用它:

 新的getCategory()执行();
新的getArea()executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

请帮我关于这个问题的。我希望每次调用它,或者它也是确定的,如果后的getCategory 的getArea()()调用
我没有得到错误。应用程序可能会做太多的工作。结果
logcat的:

  03-13 21:21:43.134:I /编舞(1222):39跳过帧!该应用程序可能会做它的主线程的工作太多了。
03-13 21:21:43.394:I /编舞(1222):43跳过帧!该应用程序可能会做它的主线程的工作太多了。
03-13 21:21:43.634:I /编舞(1222):跳过36帧!该应用程序可能会做它的主线程的工作太多了。
03-13 21:21:43.784:I /编舞(1222):38跳过帧!该应用程序可能会做它的主线程的工作太多了。
03-13 21:21:44.004:I /编舞(1222):56跳过帧!该应用程序可能会做它的主线程的工作太多了。
03-13 21:21:44.164:I /编舞(1222):跳过36帧!该应用程序可能会做它的主线程的工作太多了。
03-13 21:21:45.495:I /编舞(1222):33跳过帧!该应用程序可能会做它的主线程的工作太多了。
03-13 21:21:45.765:I /编舞(1222):43跳过帧!该应用程序可能会做它的主线程的工作太多了。
03-13 21:21:46.0​​05:I /编舞(1222):跳过36帧!该应用程序可能会做它的主线程的工作太多了。
03-13 21:21:46.235:I /编舞(1222):44跳过帧!该应用程序可能会做它的主线程的工作太多了。
03-13 21:21:46.525:I /编舞(1222):跳过48帧!该应用程序可能会做它的主线程的工作太多了。
03-13 21:21:46.855:I /编舞(1222):39跳过帧!该应用程序可能会做它的主线程的工作太多了。
03-13 21:21:47.005:I /编舞(1222):39跳过帧!该应用程序可能会做它的主线程的工作太多了。
03-13 21:21:47.156:I /编舞(1222):37跳过帧!该应用程序可能会做它的主线程的工作太多了。
03-13 21:21:47.306:I /编舞(1222):38跳过帧!该应用程序可能会做它的主线程的工作太多了。

我在两个结合微调 AsynTask 。结果
我的code:

 区域[] = areaList gson.fromJson(因此,区域[]类);        清单<面积和GT; LLIST = Arrays.asList(areaList);
        区objArea;        areaId.add(0);
        areaName.add(选择区);        的for(int i = 0; I< lList.size();我++){
            objArea = lList.get(ⅰ);
            areaName.add(objArea.getAreaName());
            areaId.add(objArea.getAreaId());
        }        ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(
                        getActivity(),android.R.layout.simple_dropdown_item_1line,AREANAME);
                ((微调)spnCEArea).setAdapter(适配器);


解决方案

为什么你不能调用 .execute()方法的原因>的AsyncTask 在同一时间在同一个项目中是Android的,因为姜饼引入的修改。直到这个版本中,你能叫 .execute(),你需要为他们每个人都是在单独的发<运行许多次/ code>。

但由于这个版本,它们依次称为所以,如果一个是执行,另外一个也不会,如果你只是调用start .execute()

因此​​,基本上所有你需要做的是检查设备的版本,你在运行的AsyncTask 和运行一个或其他命令依赖它。

 如果(Build.VERSION.SDK_INT&LT; = Build.VERSION_ codeS.HONEYCOMB_MR1)
  your_asynctask.execute(your_params);
其他
  your_asynctask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,your_params);

更多信息<一个href=\"http://www.jayway.com/2012/11/28/is-androids-asynctask-executing-tasks-serially-or-concurrently/\">here.

顺便说一句,在应用程序可能会做太多的工作在其主线程。行不相关的的AsyncTask 。这些任务在后台中运行,如果你做你的 doInBackground()方法内的工作,这不是问题。

一个稍微不同的情况而定,如果你做你的 onPostExecute()任务了一些工作,因为这可能与你的主UI,但我们可以'不要告诉你更不知道你正在运行的code。

I want to call multiple AsynTask. It was not working so I google for some help. I found that If I use AsyncTask.THREAD_POOL_EXECUTOR then it will work.

But It is not working.

I am calling it as below:

new GetCategory().execute();
new GetArea().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

Please help me regarding this issue. I want to call it at a time or it is also ok if GetArea() call after GetCategory(). I am not getting error. Application may be doing too much work.
Logcat:

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.

I am binding Spinner on both AsynTask.
My Code :

        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);

解决方案

The reason why you can't call two .execute() methods on AsyncTask within the same project at the same time is the modification that Android introduced since Gingerbread. Up until this version, you were able to call .execute() as many times you needed as each of them were run in a separate Thread.

But as of this version, they're called sequentially so if one is executing, the other one won't start if you just call .execute().

So basically all you need to do is to check the version of the device you're running the AsyncTask on and run one or other command depending at it.

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);

More info here.

By the way, the The application may be doing too much work on its main thread. lines are not related to the AsyncTask. These tasks are being run in background and if you're doing the work within your doInBackground() method, it's not the issue.

A slightly different case might be if you're doing some work in your onPostExecute() task, as this might be related to your main UI, but we can't tell you more without knowing the code you're running.

这篇关于同时调用多个AsyncTask的Andr​​oid中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆