android项目AsyncTask类方法不执行问题

AndroidObject 2012-05-10 03:06:38
我的一个项目,用到很多异步类AsyncTask来处理,但是所有这些类中的onPostExecute方法都不被执行,还有在好多4.0一下机型上运行都有该问题,但是在一个4.0.3的系统上运行就没问题。不知道为什么,怀疑是项目环境的问题,有遇到的同行给指点一二。
...全文
947 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyng307592412 2013-11-19
  • 打赏
  • 举报
回复
引用 8 楼 anroidfinalbreak 的回复:
找到问题的原因了,这个问题是由于在服务中调用了这个一步任务类所导致的。 [Quote=引用 7 楼 的回复:] 小弟之前遇到同样问题~~~ 解决办法 换了一种 异步加载 android 异步加载 貌似有 3-4种方法 Handler Runnable == [/Quote]
什么意思
sunsh1989 2012-05-14
  • 打赏
  • 举报
回复
我也有类似的问题,大家帮帮忙:http://topic.csdn.net/u/20120514/10/87c07b17-385a-4b73-a789-18bed8a16630.html?83984
AndroidObject 2012-05-14
  • 打赏
  • 举报
回复
找到问题的原因了,这个问题是由于在服务中调用了这个一步任务类所导致的。
[Quote=引用 7 楼 的回复:]
小弟之前遇到同样问题~~~ 解决办法 换了一种 异步加载 android 异步加载 貌似有 3-4种方法 Handler Runnable ==
[/Quote]
ibb-yes 2012-05-10
  • 打赏
  • 举报
回复
小弟之前遇到同样问题~~~ 解决办法 换了一种 异步加载 android 异步加载 貌似有 3-4种方法 Handler Runnable ==
AndroidObject 2012-05-10
  • 打赏
  • 举报
回复
看看大家有没有遇到过,代码没有问题,别的项目都没有问题,只有这个项目里所有的Task类有这个问题,以前也没有问题滴。估计是项目该环境了.[Quote=引用 4 楼 的回复:]
没有代码无法定位
[/Quote]
komatineni 2012-05-10
  • 打赏
  • 举报
回复
给你一个异步下载图片的demo。

public class DownloadImageTask extends AsyncTask<String, Integer, Bitmap> {
private Context mContext; // reference to the calling Activity
int progress = -1;
Bitmap downloadedImage = null;

DownloadImageTask(Context context) {
mContext = context;
}

// Called from main thread to re-attach
protected void setContext(Context context) {
mContext = context;
if(progress >= 0) {
publishProgress(this.progress);
}
}

protected void onPreExecute() {
progress = 0;
// We could do some other setup work here before doInBackground() runs
}

protected Bitmap doInBackground(String... urls) {
Log.v("doInBackground", "doing download of image...");
return downloadImage(urls);
}

protected void onProgressUpdate(Integer... progress) {
TextView mText = (TextView)
((Activity) mContext).findViewById(R.id.text);
mText.setText("Progress so far: " + progress[0]);
}

protected void onPostExecute(Bitmap result) {
if(result != null) {
downloadedImage = result;
setImageInView();
}
else {
TextView errorMsg = (TextView)
((Activity) mContext).findViewById(R.id.errorMsg);
errorMsg.setText("Problem downloading image. Please try later.");
}
}

public Bitmap downloadImage(String... urls)
{
HttpClient httpClient = CustomHttpClient.getHttpClient();
try {
HttpGet request = new HttpGet(urls[0]);
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(params, 60000); // 1 minute
request.setParams(params);

setProgress(25);

HttpResponse response = httpClient.execute(request);

setProgress(50);

sleepFor(5000); // five second sleep

byte[] image = EntityUtils.toByteArray(response.getEntity());

setProgress(75);

Bitmap mBitmap = BitmapFactory.decodeByteArray(image, 0, image.length);

setProgress(100);

return mBitmap;
} catch (IOException e) {
// covers:
// ClientProtocolException
// ConnectTimeoutException
// ConnectionPoolTimeoutException
// SocketTimeoutException
e.printStackTrace();
}
return null;
}

private void setProgress(int progress) {
this.progress = progress;
publishProgress(this.progress);
}

protected void setImageInView() {
if(downloadedImage != null) {
ImageView mImage = (ImageView)
((Activity) mContext).findViewById(R.id.image);
mImage.setImageBitmap(downloadedImage);
}
}

private void sleepFor(long msecs) {
try {
Thread.sleep(msecs);
} catch (InterruptedException e) {
Log.v("sleep", "interrupted");
}
}
}
auroste 2012-05-10
  • 打赏
  • 举报
回复
没有代码无法定位
AndroidObject 2012-05-10
  • 打赏
  • 举报
回复
就是调用异步类onPreExecute()doInBackground()方法执行完之后,onPostExecute()就不执行了。
AndroidObject 2012-05-10
  • 打赏
  • 举报
回复
无具体代码,情况就是这么个情况。
[Quote=引用 1 楼 的回复:]
楼主最好把代码贴出来。
[/Quote]
komatineni 2012-05-10
  • 打赏
  • 举报
回复

楼主最好把代码贴出来。

80,493

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧