1 在主线程中访问网络会报异常
2 异步任务 myTask.Cancle(True);
】调用这个方法,停止子线程的操作,之后会调用异步任务的onCancle()方法,不会调用onPostExecute()方法,通过myTask.isCancelled方法判断是否被取消。
package com.example.kk;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
bt=(Button)findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new myTask().execute();
}
});
}
/**
* 创建异步任务
* Params:主线程传过来的参数类型
* Progress:打印进度的参数类型
* Result:子线程结束返回的数据类型
* 】没有参数设置为Void
*/
class myTask extends AsyncTask<Void, Integer, String>{
/**
* 在子线程执行前执行(属于主线程)
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.i("MainActivity","onPreExecute");
}
/**
* 子线程,进行下载任务
*/
@Override
protected String doInBackground(Void... params) {
for(int i=0;i<10;i++){
//该方法调用onProgressUpdate进行进度的操作
publishProgress(i);
}
return "df";
}
/**
* 子线程结束后执行(属于主线程)
*/
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.i("MainActivity",result);
}
/**
* 进行进度的操作,什么时候调用,什么时候执行(属于主线程)
*/
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.i("MainActivity",values[0]+"");
}
}
}
异步任务带进度条下载图片
package com.example.kk;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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 org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
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.ImageView;
public class MainActivity extends Activity {
private Button bt;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
img = (ImageView) findViewById(R.id.img);
bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new myTask(MainActivity.this).execute();
}
});
}
/**
* 创建异步任务 Params:主线程传过来的参数类型 Progress:打印进度的参数类型 Result:子线程结束返回的数据类型
* 】没有参数设置为Void
*/
class myTask extends AsyncTask<Void, Integer, byte[]> {
/**
* 在子线程执行前执行(属于主线程)
*/
private Context context;
ProgressDialog dialog=null;
public myTask(Context context){
this.context=context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog=new ProgressDialog(context);
dialog.setTitle("进度");
dialog.setMessage("正在玩命加载中");
dialog.setIcon(R.drawable.ic_launcher);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.show();
}
/**
* 子线程,进行下载任务
*/
@Override
protected byte[] doInBackground(Void... params) {
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://i6.topit.me/6/5d/45/1131907198420455d6o.jpg");
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity=response.getEntity();
long contentLen=entity.getContentLength();
InputStream in=entity.getContent();
ByteArrayOutputStream out=new ByteArrayOutputStream();
byte b[]=new byte[1024];
int len=0;
int sumLen=0;
while((len=in.read(b))!=-1){
sumLen+=len;
out.write(b,0,len);
int progress=(int) (sumLen*100/contentLen);
publishProgress(progress);
}
return out.toByteArray();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 子线程结束后执行(属于主线程)
*/
@Override
protected void onPostExecute(byte[] result) {
super.onPostExecute(result);
if (result != null) {
dialog.dismiss();
img.setImageBitmap(BitmapFactory.decodeByteArray(result, 0,
result.length));
}
}
/**
* 进行进度的操作,什么时候调用,什么时候执行(属于主线程)
*/
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
dialog.setProgress(values[0]);
}
}
}