package com.example.jsontest;
import java.io.File;
import java.util.HashMap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements IGAsyncTaskInterface, View.OnClickListener {
private Button m_downBtn;
private Button m_downCancelBtn;
private ProgressBar m_progressBar;
private TextView m_textView;
private IGAsyncDownloadTask m_currTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_downBtn = (Button)findViewById(R.id.TestDownBtn);
m_downCancelBtn = (Button)findViewById(R.id.TestDownCancel);
m_progressBar = (ProgressBar)findViewById(R.id.TestProgressBar);
m_textView = (TextView)findViewById(R.id.TestTextView);
m_downBtn.setOnClickListener(this);
deleteDownloadedFile();
}
protected void deleteDownloadedFile()
{
File dir = new File(getApplicationStoragePath() + "/pepolds1.zip");
if(dir.exists())
dir.delete();
}
protected String getApplicationStoragePath() {
String ext = Environment.getExternalStorageState();
String path = Environment.getExternalStorageDirectory().toString();
if(ext.equals(Environment.MEDIA_MOUNTED))
{
path = Environment.getExternalStorageDirectory().getAbsolutePath();
path = path + "/" + "pepolds1";
}
else
{
path = Environment.MEDIA_UNMOUNTED;
}
return path;
}
protected String getFileSize(int size)
{
String appendix = "Byte";
int calSize = size;
if(size > 1024 && size < 1024 * 1024)
{
appendix = "KB";
calSize = size / 1024;
}
else if(size > 1024 * 1024)
{
appendix = "MB";
calSize = size / (1024 * 1024);
}
return String.valueOf(calSize) + appendix;
}
private void startDownload() {
if(m_currTask == null)
{
File dir = new File(getApplicationStoragePath());
if(!dir.exists())
dir.mkdirs();
String[] params = { "http://gdsgdsgdsgf/"+"sgddgsmp4",
null,
getApplicationStoragePath() + "/pepolds1.zip" };
m_currTask = new IGAsyncDownloadTask(this, false);
m_currTask.execute(params);
}
}
private void cancelDownload() {
if(m_currTask != null &&
(m_currTask.getStatus() == AsyncTask.Status.PENDING ||
m_currTask.getStatus() == AsyncTask.Status.RUNNING))
{
m_currTask.cancel(true);
m_currTask = null;
}
}
@Override
public void onPreExecute() {
Toast.makeText(this, "Start Download!", Toast.LENGTH_SHORT).show();
}
@Override
public void onProgressUpdate(Integer... values) {
float curr = (float)m_progressBar.getProgress() + (float)values[0];
float total = (float)values[1];
float fPercent = curr / total;
int percent = (int)(fPercent * 100);
m_progressBar.setMax(values[1]);
m_progressBar.setProgress(m_progressBar.getProgress() + values[0]);
m_textView.setText(getFileSize(values[0] + m_progressBar.getProgress()) + "/" + getFileSize(values[1]) + " [" + String.valueOf(percent) + "%]");
}
@Override
public void onPostExecute(IGAsyncNetworkStatusEnum status) {
m_currTask = null;
Toast.makeText(this, "Download Complete!", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(IGAsyncNetworkStatusEnum status) {
m_currTask = null;
Toast.makeText(this, "Download Cancelled!", Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.TestDownBtn:
{
startDownload();
}
break;
case R.id.TestDownCancel:
{
cancelDownload();
}
break;
}
}
}
프로그레스바가 계속 진행 되며, 앱을 빠져나가고 파일을 보니까 다운로드가 중지가 되서 파일이 회색이 된것을 확인 했습니다.
프로그레스바도 같이 중지시킬 방법이 없을까요??