AsyncTask模仿下载

package com.example.asynctask1;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		findViewById(R.id.bn_xz).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 执行下载任务
				// url
				String url = "http://192.168.1.221:8080/Android1/GridView.mp4";
				// String url =
				// "http://192.168.1.222:8080/T02filedownloa/servlet/AServlet";
				// 执行耗时任务
				new AsyncTask<String, Integer, Boolean>() {

					private ProgressDialog dialog;

					// 在执行耗时任务之前做一些准备工作,比如显示一个下载进度对话框
					@Override
					protected void onPreExecute() {
						super.onPreExecute();
						dialog = new ProgressDialog(MainActivity.this);
						dialog.setTitle("下载文件");
						dialog.setMessage("正在下载中,请稍后...");
						dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
						dialog.setIndeterminate(false);
						dialog.show();

					}

					// 做耗时任务的
					@Override
					protected Boolean doInBackground(String... params) {
						try {
							HttpURLConnection con = (HttpURLConnection) new URL(
									params[0]).openConnection();
							int responseCode = con.getResponseCode();

							if (responseCode == 200) {
								int length = con.getHeaderFieldInt("length", 0);
								String fileName = con
										.getHeaderField("fileName");
								Log.i("--Main--", "length=" + length
										+ ",fileName=" + fileName);
								int length2 = con.getContentLength();
								dialog.setMax(length2);// 设置进度条最大值

								BufferedInputStream bis = new BufferedInputStream(
										con.getInputStream());
								byte[] bytes = new byte[1024 * 1024 * 10];
								int len = -1;
								int currentLength = 0;
								while ((len = bis.read(bytes)) != -1) {
									currentLength += len;
									// 通知进度条更新
									publishProgress(currentLength);
								}

								return true;
							} else {
								Log.i("--Main--", "responseCode:"
										+ responseCode);
							}

						} catch (MalformedURLException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
						return false;
					}

					// 更新当前进度
					@Override
					protected void onProgressUpdate(Integer... values) {
						super.onProgressUpdate(values);
						/*
						 * if(!dialog.isShowing()){ dialog.show(); }
						 */
						dialog.setProgress(values[0]);
					}

					@Override
					protected void onPostExecute(Boolean result) {
						// TODO Auto-generated method stub
						super.onPostExecute(result);
						if (result) {
							Toast.makeText(MainActivity.this, "下载完成",
									Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(MainActivity.this, "下载失败",
									Toast.LENGTH_SHORT).show();
						}
						dialog.dismiss();
					}

					// 执行完成,根据结果更新UI

				}.execute("http://192.168.1.221:8080/Android1/asd.mp4");
			}
		});
	}

}


猜你喜欢

转载自blog.csdn.net/qq_42120002/article/details/80188017