AsyncTask应用示例

 package com.example.testdemo;

 import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; 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.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends Activity { private Button btn;
private ImageView mg;
private ProgressDialog pDialog;
private String path="http://himg2.huanqiu.com/attachment2010/111207/zip1323221781/1323221781_6.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button1);
mg=(ImageView)findViewById(R.id.imageView1);
pDialog=new ProgressDialog(this);
pDialog.setTitle("提示");
pDialog.setCancelable(false);
pDialog.setMessage("正在下载,请稍后……");
pDialog.setIcon(R.drawable.ic_launcher);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
new MyTask().execute(path);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
} public class MyTask extends AsyncTask<String, Integer, Bitmap>
{ /* (non-Javadoc)
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
Bitmap bitmap=null;
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
InputStream inputStream=null;
try {
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(params[0]);
HttpResponse httpResponse=httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode()==200) {
inputStream=httpResponse.getEntity().getContent();
long file_length=httpResponse.getEntity().getContentLength();
byte[] data=new byte[1024];
int len=0;
int total=0;
while ((len = inputStream.read(data)) != -1) {
total+=len;
int values=(int)((total/(float)file_length)*100);
publishProgress(values);
outputStream.write(data, 0, len); }
byte[] result01=outputStream.toByteArray();
bitmap=BitmapFactory.decodeByteArray(result01, 0, result01.length);
} } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return bitmap;
} @Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pDialog.dismiss();
mg.setImageBitmap(result); } @Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog.show();
} @Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
pDialog.setProgress(values[0]);
} } }

apk下载点击这里TestDemo.apk

上一篇:JS高级程序设计学习笔记1


下一篇:《javascript个人理解,个人整理。》