首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android异步任务

android异步任务
EN

Stack Overflow用户
提问于 2011-04-12 02:44:07
回答 1查看 1.6K关注 0票数 1

我有一个应用程序,它从web服务器获取特定日期的数据作为响应,并根据已经到来的数据(数字/时间)绘制o/p图。

现在,我正在调用web服务器作为异步任务。

我还可以跨多个屏幕(前一天/第二天)多次调用web-server (多个异步任务),以便从服务器获取数据。

问题是当跳转(异步任务)的数量增加时,应用程序会显示ANR。

有没有更好的方法来处理这些场景,而不是为每个web服务器调用(Fling)创建异步任务。

添加部分代码:

代码语言:javascript
复制
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE 
          && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
      valuesDayWithTime=onStart(datePrevious);
}



public GraphView onStart(String date){
    urlFinal=createUrl();
    new DownloadDataTask(this).execute(urlFinal);
}

private class DownloadDataTask extends AsyncTask<String, Integer, Long> {

    YieldActivity yActivity;
    ProgressBar pBar ;
    DownloadDataTask(YieldActivity act){
        yActivity=act;
    }

    protected void onPreExecute() {
        relLay=(RelativeLayout) findViewById(R.id.graphView);
        pBar
        = new ProgressBar(yActivity);
        LayoutParams lp =new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
        pBar.setLayoutParams(lp);
        pBar.setMax(100);
        relLay.removeAllViews();
        relLay.addView(pBar);
    }

    protected Long doInBackground(String... urls) {
        int totalCount = urls.length;
        for (int i = 0; i < totalCount; i++) {
            publishProgress((int) ((i / (float) totalCount) * 100));
            downloadFromUrl(urls[i]);
        }
        return (long) totalCount;
    }

    protected void onProgressUpdate(Integer... progress) {
        Log.i(progress[0] +"%");
    }

    protected void onPostExecute(Long result) {

        graphViewDay=calculate(dateNow,valuesDayWithTime);
        relLay.removeView(pBar);
        relLay.addView(graphViewDay);


    }
}

public void downloadFromUrl(String fromURL) {
    try {
        getHttpResponse(fromURL.toString());

        parseResponse();
        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-12 02:52:44

只要在主线程上创建AsyncTask对象并从主线程调用execute,就不会有任何问题。你能为你的AsyncTask发布一些伪代码吗?你可以在AsyncTask here上找到更多。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5625933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档