android异步任务
问题描述:
我有一个应用程序获取特定日期的数据作为来自Web服务器的响应,并根据已经到达的数据(数字/时间)绘制o/p图。android异步任务
现在我正在调用网络服务器作为异步任务。
我也可以在多个屏幕上(前一天/第二天)多次调用Web服务器(多个异步任务),以便从服务器获取数据。
问题是当应用程序显示ANR的应答数(异步任务)增加时。
有没有更好的方法来处理这些场景,而不是为每个web服务器调用(fling)创建异步任务。代码
添加部分:
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();
}
}
请发布适合您的解决方案,以便其他人可以参考。 – 2011-04-21 16:02:44