android AsyncTask 비동기 다운로드 및 진행 항목 업데이트

AsyncTask          
 
 //                  
AsyncTask<String, Integer, String>  

     :String            

     :Integer         

     :String       ,           ,      BitMap




      :


package com.example.testcctv;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpStatus;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.ProgressBar;

/*****************************************************************************************************************************************
 *             :      :String                  :Integer              :String
 *       ,           ,      BitMap
 *****************************************************************************************************************************************/
public class DownloadAsyncTask extends AsyncTask<String, Integer, String> {

	private final ProgressBar bar;
	private int count = 0;

	public DownloadAsyncTask(ProgressBar bar) {
		super();
		this.bar = bar;
	}

	@Override
	protected String doInBackground(String... params) {
		try {
			URL url = new URL(params[0]);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setConnectTimeout(10 * 1000);
			connection.connect();
			if (connection.getResponseCode() == HttpStatus.SC_OK) {
				bar.setMax(connection.getContentLength());
				File file = new File(params[1]);
				file.createNewFile();
				InputStream inputStream = connection.getInputStream();
				ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
				byte[] buffer = new byte[10 * 1024];
				while (true) {
					int len = inputStream.read(buffer);
					publishProgress(len);
					if (len == -1) {
						break;
					}
					arrayOutputStream.write(buffer, 0, len);
				}
				arrayOutputStream.close();
				inputStream.close();

				byte[] data = arrayOutputStream.toByteArray();
				FileOutputStream fileOutputStream = new FileOutputStream(file);
				fileOutputStream.write(data);
				fileOutputStream.close();
			}

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

	@Override
	protected void onCancelled() {
		// TODO Auto-generated method stub
		super.onCancelled();
	}

	@Override
	protected void onPostExecute(String result) {
		Log.v("Fover", result);
		super.onPostExecute(result);
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		count += values[0];
		bar.setProgress(count);
		super.onProgressUpdate(values);
	}

}

    : 


//         ,         
				String[] downLoadPath = {
						HTTPURL,
						Environment.getExternalStorageDirectory()
								+ "/dujinyang/dudu.apk" };
				DownloadAsyncTask asynTask = new DownloadAsyncTask(progressBar);
				asynTask.execute(downLoadPath);

좋은 웹페이지 즐겨찾기