android AsyncTask
16lz
2021-12-04
1. http://android.tgbus.com/Android/tutorial/201109/369193.shtml
2.Android中AsyncTask的用法实例
(比较深入潜出了....膜拜一下.)
http://www.pin5i.com/showtopic-android-asynctask-sample.html
3.Android AsyncTask 详解
http://dev.10086.cn/cmdn/wiki/index.php?edition-view-5195-1.html
4.Android Threads, Handlers and AsyncTask - Tutorial
http://www.vogella.de/articles/AndroidPerformance/article.html
5.华清远见 AsyncTask视频讲解
http://v.youku.com/v_show/id_XMzExNjU0MDgw.html
6.Android如何异步获取图片
http://www.cnblogs.com/budoudou/archive/2011/07/13/2105007.html
7.异步下载图片
http://www.cnblogs.com/zhangdongzi/archive/2011/04/13/2015215.html#2311099
自己学习了AsyncTask后的例子:
2.Android中AsyncTask的用法实例
(比较深入潜出了....膜拜一下.)
http://www.pin5i.com/showtopic-android-asynctask-sample.html
3.Android AsyncTask 详解
http://dev.10086.cn/cmdn/wiki/index.php?edition-view-5195-1.html
4.Android Threads, Handlers and AsyncTask - Tutorial
http://www.vogella.de/articles/AndroidPerformance/article.html
5.华清远见 AsyncTask视频讲解
http://v.youku.com/v_show/id_XMzExNjU0MDgw.html
6.Android如何异步获取图片
http://www.cnblogs.com/budoudou/archive/2011/07/13/2105007.html
7.异步下载图片
http://www.cnblogs.com/zhangdongzi/archive/2011/04/13/2015215.html#2311099
自己学习了AsyncTask后的例子:
package com.asynctaskdemo;import java.io.InputStream;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.ProgressBar;import android.widget.TextView;public class UpdateImgActivity extends Activity implements OnClickListener {private TextView tv_desc,tv_percent;private EditText et_imgurl;private Button btn_start;private ImageView iv_result;private ProgressBar pb;@Overrideprotected void onCreate(Bundle bd){super.onCreate(bd);setContentView(R.layout.updateimg);tv_desc = (TextView)findViewById(R.id.tv_desc);tv_percent = (TextView)findViewById(R.id.tv_percent);et_imgurl = (EditText)findViewById(R.id.et_imgurl);btn_start = (Button)findViewById(R.id.btn_start);iv_result = (ImageView)findViewById(R.id.iv_fromNet);pb = (ProgressBar)findViewById(R.id.pb);btn_start.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch(v.getId()){case R.id.btn_start:{if(!et_imgurl.getText().toString().equals("")){new UpdateImgTask().execute(et_imgurl.getText().toString());}elseet_imgurl.setError("不能为空");}}}class UpdateImgTask extends AsyncTask<String, Integer, Bitmap>{//首先会被调用@Overrideprotected void onPreExecute() {// TODO Auto-generated method stubsuper.onPreExecute();tv_desc.setText("从网络更新图片:开始");}@Overrideprotected Bitmap doInBackground(String... params) {// TODO Auto-generated method stubtry {HttpClient client = new DefaultHttpClient();// params[0] 代表连接的urlHttpGet get = new HttpGet(params[0]);HttpResponse response = client.execute(get);HttpEntity entity = response.getEntity();long length = entity.getContentLength();InputStream is = entity.getContent();/*if (is != null) {ByteArrayOutputStream baos = new ByteArrayOutputStream();byte[] buf = new byte[128];int ch = -1;int count = 0;while ((ch = is.read(buf)) != -1) {baos.write(buf, 0, ch);count += ch;if (length > 0) {// 如果知道响应的长度,调用publishProgress()更新进度publishProgress((int) ((count / (float) length) * 100));}// 为了在模拟器中清楚地看到进度,让线程休眠100msThread.sleep(100);}}*/for(int i=0;i<=100;i++){pb.setProgress(i);publishProgress(i);try {Thread.sleep(i);} catch (InterruptedException e) {e.printStackTrace();}}// 返回结果return BitmapFactory.decodeStream(is);} catch (Exception e) {e.printStackTrace();}return null;}@Overrideprotected void onProgressUpdate(Integer... values) {// TODO Auto-generated method stub//super.onProgressUpdate(values);tv_percent.setText(values[0]+"%");pb.setProgress(values[0]);}@Overrideprotected void onPostExecute(Bitmap result) {// TODO Auto-generated method stubsuper.onPostExecute(result);if(result!=null)iv_result.setImageBitmap(result);elsetv_desc.setText("从网络更新图片:失败");}}}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_desc" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="从网络更新图片" /><EditText android:id="@+id/et_imgurl" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="http://tp4.sinaimg.cn/2137426403/180/5605866249/1" /> <Button android:id="@+id/btn_start" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="start" /> <TextView android:id="@+id/tv_percent" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <ProgressBar android:id="@+id/pb" android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal"/> <ImageView android:id="@+id/iv_fromNet" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
更多相关文章
- android图片缩放手势检测类--ScaleGestureDetector
- android图片压缩方法
- Android(安卓)SDK下载和更新失败的解决方法
- android ListView SimpleAdapter 带图片
- 【Android】Android内存溢出问题---用自行开辟的空间进行对内存
- android -上传文件到服务器
- 笔记——Android(安卓)中的小细节
- Android之OnGestureListener实现图片的左右滑动
- TabActivity的使用遇到的问题