自動檢測更新apk版本問
阿新 • • 發佈:2019-02-02
public static File getApkFromServer(String path,ProgressDialog dialog,String apkName)
throws Exception{
//apkname=xxx.apk
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
URL url =new URL(path);
HttpURLConnection conn =(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000); //請求訪問超時時間
//獲取到檔案的大小 和對話方塊最大值繫結
dialog.setMax(conn.getContentLength());
InputStream is = conn.getInputStream();
File file = new File(Environment.getExternalStorageDirectory(), apkName);
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len ;
int total=0;
while((len =bis.read(buffer))!=-1){
fos.write(buffer, 0, len);
total+= len;
//獲取當前下載量
dialog.setProgress(total);
}
fos.close();
bis.close();
is.close();
return file;
}
else{
return null;
}
}
//apkname=xxx.apk
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
URL url =new URL(path);
HttpURLConnection conn =(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000); //請求訪問超時時間
//獲取到檔案的大小 和對話方塊最大值繫結
dialog.setMax(conn.getContentLength());
InputStream is = conn.getInputStream();
File file = new File(Environment.getExternalStorageDirectory(), apkName);
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len ;
int total=0;
while((len =bis.read(buffer))!=-1){
fos.write(buffer, 0, len);
total+= len;
//獲取當前下載量
dialog.setProgress(total);
}
fos.close();
bis.close();
is.close();
return file;
}
else{
return null;
}
}