@Override
public void run() {
try {
// 檢測設備有無SD卡
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
apkFile = Environment.getExternalStorageDirectory() + Constants.SAVEPATH + Constants.PROJECTNAME + SharedPreferenceSetting.getApkVersionCode(this) + ".apk";
} else {
myHandler.sendEmptyMessage(3);
return;
}
URL url = new URL(SharedPreferenceSetting.getAPKURL(this));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream(); android
File file = new File(Environment.getExternalStorageDirectory() + Constants.SAVEPATH);
if (!file.exists()) {
file.mkdir();
}
File ApkFile = new File(apkFile);
FileOutputStream fos = new FileOutputStream(ApkFile);
int count = 0;
byte buf[] = new byte[1024];
do {
int numread = is.read(buf);
count += numread;
downloadProgressValue = (int) (((float) count / length) * 100);
// 更新進度
myHandler.sendEmptyMessage(1);
if (numread <= 0) {
// 下載完成通知安裝
myHandler.sendEmptyMessage(2);
break;
}
fos.write(buf, 0, numread);
} while (!interceptFlag);// 點擊取消就中止下載. ide
fos.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} this
} url