public
class
MainActivity
extends
Activity {
private
ProgressDialog progressDialog;
public
static
final
int
progressbarType =
0
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public
void
mOnClick(View v) {
ApkDownload apk =
new
ApkDownload();
apk.start();
}
@Override
protected
Dialog onCreateDialog(
int
id) {
switch
(id) {
case
progressbarType:
progressDialog =
new
ProgressDialog(
this
);
progressDialog.setMessage(
"파일을 다운로드 중입니다. 잠시만 기다려 주십시오..."
);
progressDialog.setIndeterminate(
false
);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(
true
);
progressDialog.show();
return
progressDialog;
default
:
return
null
;
}
}
protected
void
onProgressUpdate(String... progress) {
progressDialog.setProgress(Integer.parseInt(progress[
0
]));
}
}
class
ApkDownload
extends
Thread {
public
ApkDownload() {
super
();
}
public
void
run() {
String apkName =
"apk이름"
;
try
{
URL url =
new
URL(
"http://apk경로/app/"
+apkName+
".apk"
);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
File sdCardRoot = Environment.getExternalStorageDirectory();
File file =
new
File(sdCardRoot, apkName+
".apk"
);
InputStream in = urlConnection.getInputStream();
FileOutputStream fos =
new
FileOutputStream(file);
int
totalSize= urlConnection.getContentLength();
int
downloadedSize =
0
;
byte
[] buffer =
new
byte
[totalSize];
int
bufferLength =
0
;
while
((bufferLength = in.read(buffer)) >
0
) {
fos.write(buffer,
0
, bufferLength);
downloadedSize += bufferLength;
}
fos.close();
}
catch
(MalformedURLException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}