URL url = new URL(info.getUrl());
// URL url = new URL(surl);
interceptFlag = false;
// InetAddress ip = InetAddress.getByName("deamo.5ydian.com");
// Log.e(TAG, ip.toString());
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File ApkFile = new File(savePath, apkname);
if (ApkFile.exists())
ApkFile.delete();
ApkFile.createNewFile();
FileOutputStream fos = new FileOutputStream(ApkFile);
int count = 0;
byte buf[] = new byte[1024];
int times = 0;
do {
int numread = is.read(buf);
count += numread;
if ((times == 512) || (count == length)) {
progress = (int) (((float) count / length) * 100);
if (DEBUG)
Log.e(TAG, "download process = " + progress);
handler.sendEmptyMessage(DOWN_UPDATE);
times = 0;
// continue;
}
times++;
if (numread <= 0) {
// 下载完成通知安装
fos.flush();
fos.close();
is.close();
interceptFlag = true;
break;
}
fos.write(buf, 0, numread);
} while (!interceptFlag);// 点击取消就停止下载.
if (DEBUG)
Log.e(TAG, "下载完成--");
} else {
String apkPath = "/data/data/" + mContext.getPackageName()
+ "/files";
File file = new File(apkPath, apkname);
if (file.exists())
file.delete();
file.createNewFile();
FileOutputStream os = new FileOutputStream(file);
int times = 0;
int count = 0;
byte[] buf = new byte[1024];
do {
int numread = is.read(buf);
count += numread;
if ((times == 512) || (count == length)) {
progress = (int) (((float) count / length) * 100);
if (DEBUG)
Log.e(TAG, "download process = " + progress);
handler.sendEmptyMessage(DOWN_UPDATE);
times = 0;
// continue;
}
times++;
if (numread <= 0) {
// 下载完成通知安装
os.flush();
os.close();
is.close();
interceptFlag = true;
break;
}
//从网络下载写入文件时,一定要注意使用此方式写入
os.write(buf, 0, numread);
} while (!interceptFlag);// 点击取消就停止下载.
if (DEBUG)
Log.e(TAG, "下载完成--");
}
// 安装更新后的软件
Message msg = new Message();
msg.what = DOWN_OVER;
handler.sendMessageDelayed(msg, 2000);
从网络下载apk 到 rom 或者 sd 卡
最新推荐文章于 2024-09-17 09:16:00 发布