下載
private void downloadApp() {
new Thread(new Runnable() {
@Override
public void run() {
URL url = null;
InputStream in = null;
FileOutputStream out = null;
HttpURLConnection conn = null;
try {
url = new URL(spec);
conn = (HttpURLConnection) url.openConnection();
conn.connect();
long fileLength = conn.getContentLength();
in = conn.getInputStream();
File filePath = new File(FILE_PATH);
if (!filePath.exists()) {
filePath.mkdir();
}
out = new FileOutputStream(new File(FILE_NAME));
byte[] buffer = new byte[1024];
int len = 0;
long readedLength = 0l;
while ((len = in.read(buffer)) != -1) {
// 用戶點擊“取消”按鈕,下載中斷
if (isCancel) {
break;
}
out.write(buffer, 0, len);
readedLength += len;
curProgress = (int) (((float) readedLength / fileLength) * 100); handler.sendEmptyMessage(UPDARE_TOKEN);
if (readedLength >= fileLength) {
progressDialog.dismiss(); // 下載完畢,通知安裝 handler.sendEmptyMessage(INSTALL_TOKEN);
break;
}
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (conn != null) {
conn.disconnect();
}
}
}
}).start();
}
安裝
private void installApp() {
File appFile = new File(FILE_NAME);
if (!appFile.exists()) {
return; }
// 跳轉(zhuǎn)到新版本應(yīng)用安裝頁面
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + appFile.toString()), "application/vnd.android.package-archive");
context.startActivity(intent);}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。