在两个不同的类在执行了两个的AsyncTask在

编程入门 行业动态 更新时间:2024-10-28 19:29:32
本文介绍了在两个不同的类在执行了两个的AsyncTask在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个不同的类2 AsyncTasks,但问题是,当第一是在不执行所述第二backgroung状态做。第一的AsyncTask如果$ P $在循环pformed因为它需要每5秒钟更新新的数据。如果我停止任务(条件= FLASE)第二个完美的作品。

第一类:

公共类MapScreen延伸活动{@覆盖保护无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_map_screen);        UpdateUserCords updateUC =新UpdateUserCords();        updateUC.execute();    }类UpdateUserCords扩展的AsyncTask<弦乐,太虚,字符串>{    @覆盖    保护字符串doInBackground(字符串... PARAMS){        而(条件)        {            //一些code在环...            尝试{                视频下载(5000);            }赶上(InterruptedException的E){                // TODO自动生成catch块                e.printStackTrace();            }        }        返回null;    }    @覆盖    在preExecute保护无效(){        super.on preExecute();    }    @覆盖    保护无效onProgressUpdate(虚空......值){        super.onProgressUpdate(值);    }    @覆盖    保护无效onPostExecute(字符串结果){        super.onPostExecute(结果);    }}}

第二类:

公共类组扩展活动{@覆盖公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_groups);    getGroups getGr =新getGroups();    getGr.execute(); //不执行,没有错误或崩溃}类getGroups扩展的AsyncTask<弦乐,太虚,字符串>{    ArrayList的<&HashMap的LT;字符串,字符串>>菜单项=新的ArrayList<&HashMap的LT;字符串,字符串>>();    @覆盖    保护字符串doInBackground(字符串... PARAMS){        //一些code ...        返回null;    }    @覆盖    在preExecute保护无效(){        super.on preExecute();    }    @覆盖    保护无效onPostExecute(字符串结果){        super.onPostExecute(结果);    }}}

解决方案

Android的API 11后,的AsyncTask 开始吧,以在默认情况下连续执行程序运行,这意味着只有一个任务同时运行。为了得到前的行为,以API 11,这是在的ThreadPoolExecutor 运行,你需要在code像这样指定它:

如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB){  myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);}其他{  myTask.execute();}

请看看这里了解更多信息:的commonsware/blog/2012/04/20/asynctask-threading-regression-confirmed.html

祝你好运!

P.S。它不推荐使用的AsyncTask 的无限线程,的AsyncTask 目的是做后台任务,不一直运行下去,所以如果你想无限线程,我想你应该将其创建为发,而不是作为一个的AsyncTask 。

I have 2 AsyncTasks in two different classes but the problem is when the first is do in backgroung state the second is not executed. The first asyncTask if preformed in loop because it needs to update every 5 seconds the new data. If i stop the task (condition = flase) the second one works perfectly.

First class:

public class MapScreen extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map_screen); UpdateUserCords updateUC = new UpdateUserCords(); updateUC.execute(); } class UpdateUserCords extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { while(condition) { //some code in loop... try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); } @Override protected void onPostExecute(String result) { super.onPostExecute(result); } } }

Second class:

public class Groups extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_groups); getGroups getGr = new getGroups(); getGr.execute(); //not executing, no error or crash } class getGroups extends AsyncTask<String, Void, String> { ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>(); @Override protected String doInBackground(String... params) { //some code... return null; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(String result) { super.onPostExecute(result); } } }

解决方案

After Android API 11, AsyncTasks started to run on serial executor by default, that means that only one task is running at a time. To get the behavior of prior to API 11, which is running on ThreadPoolExecutor, you'll need to specify it in the code like this:

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) { myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } else { myTask.execute(); }

Please take a look here for more information: commonsware/blog/2012/04/20/asynctask-threading-regression-confirmed.html

Good luck!

P.S. It's not recommended to use AsyncTask for an infinite thread, AsyncTask purpose is to do a Task on the background, not to run forever, so if you want an infinite thread, I think you should create it as a Thread and not as an AsyncTask.

更多推荐

在两个不同的类在执行了两个的AsyncTask在

本文发布于:2023-05-25 19:37:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/320858.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   AsyncTask

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!