/**
* 다운로드 처리
* @author etc.jhshin
*
*/
class DownloadFile extends AsyncTask<ArrayList<DownInfo>, Long, ArrayList<InstallInfo>> //�μ�, ���, ���
{
protected void onPreExecute(){}
/**
* 파일 다운 처리 부분
*/
protected ArrayList<InstallInfo> doInBackground(ArrayList<DownInfo>... strData)
{
TalkTalkWidgetProvider.conBoolean = true;
downBoolean = true;
downTrue = false;
URL url[] = new URL[strData[0].size()];
FileOutputStream fileOutput[] = new FileOutputStream[strData[0].size()];
InputStream inputStream[] = new InputStream[strData[0].size()];
int totalSize[] = new int[strData[0].size()];
long totalNum=0;
long afterSize=0;
long num;
progress[0] = (long)0;
progress[1] = (long)0;
progress[2] = (long)0;
progress[3] = (long)0;
progress[4] = (long)0;
try {
totalNum = strData[0].size();
for(int i=0; i<strData[0].size(); i++){
url[i] = new URL(String.valueOf(strData[0].get(i).url));
long downloadedSize = 0;
conn = (HttpURLConnection) url[i].openConnection();
fileOutput[i] = new FileOutputStream(strData[0].get(i).apkName);
inputStream[i] = conn.getInputStream();
totalSize[i] = conn.getContentLength();
int bufferLength = 0;
byte[] buffer = new byte[totalSize[i]];
while ( (bufferLength = inputStream[i].read(buffer)) > 0 ) {
if(downBoolean){
fileOutput[i].write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
afterSize += bufferLength;
progress[0] = (long)( (downloadedSize*100) /totalSize[i]);
progress[1] = afterSize;
progress[2] = totalFinalSize;
num = i+1;
progress[3] = num;
progress[4] = totalNum;
publishProgress(progress);
}else{
conn.disconnect();
conn = null;
}
}
inputStream[i].close();
fileOutput[i].close();
onInstall(strData[0].get(i).apkName);
}
conn.disconnect();
downTrue = true;
} catch (Exception e) {
e.printStackTrace();
}
return installList;
}
/**
* ProgressBar UI 처리
*/
public void onProgressUpdate(Long... args)
{
downListProgressBar.setProgress(args[0].intValue());
downListTotalProgressBar.setProgress((int)( (args[1]*100) /args[2]));
downListTotalText_textView.setText(String.valueOf((int)( (args[1]*100) /args[2]))+"%" );
downListText_textView.setText(String.valueOf(args[0])+"%" );
downNumText_textView.setText(String.valueOf(args[3]));
downTotalNumText_textView.setText(String.valueOf(args[4]));
}
/**
* 모든 파일 다운 완료 후 처리 부분
*/
protected void onPostExecute(ArrayList<InstallInfo> result)
{
Log.i(TAG, "onPostExecute()");
if(downTrue){
TalkTalkWidgetProvider.conBoolean = false;
downBoolean = false;
conn = null;
downTrue = true;
down_progress_layout.setVisibility(View.GONE);
setPopup(View.VISIBLE, "다운로드가 완료되었습니다.");
}
}
/**
* 파일 전송시 취소
*/
protected void onCancelled()
{
downBoolean = false;
if(conn!=null)
conn.disconnect();
conn = null;
}
}
'프로그래밍 정리 > 안드로이드' 카테고리의 다른 글
위젯 Refresh (0) | 2012.06.25 |
---|---|
apk 인스톨 소스 (0) | 2012.06.25 |
폴더 삭제(안의 파일들도 같이 삭제 (0) | 2012.06.25 |
APPWIDGET_RESIZE (0) | 2012.06.21 |
CustomDialog (0) | 2012.06.19 |