Android版本检测 自动更新
2010-10-19 20:17:10 来源:WEB开发网}
}
private void downloadTheFile(final String strPath) {
fileEx = strURL.substring(strURL.lastIndexOf(".") + 1, strURL.length())
.toLowerCase();
fileNa = strURL.substring(strURL.lastIndexOf("/") + 1,
strURL.lastIndexOf("."));
try {
if (strPath.equals(currentFilePath)) {
doDownloadTheFile(strPath);
}
currentFilePath = strPath;
Runnable r = new Runnable() {
public void run() {
try {
doDownloadTheFile(strPath);
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
}
};
new Thread(r).start();
} catch (Exception e) {
e.printStackTrace();
}
}
private void doDownloadTheFile(String strPath) throws Exception {
Log.i(TAG, "getDataSource()");
if (!URLUtil.isNetworkUrl(strPath)) {
Log.i(TAG, "getDataSource() It's a wrong URL!");
} else {
URL myURL = new URL(strPath);
URLConnection conn = myURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
if (is == null) {
throw new RuntimeException("stream is null");
}
File myTempFile = File.createTempFile(fileNa, "." + fileEx);
currentTempFilePath = myTempFile.getAbsolutePath();
FileOutputStream fos = new FileOutputStream(myTempFile);
byte buf[] = new byte[128];
do {
int numread = is.read(buf);
if (numread <= 0) {
break;
}
fos.write(buf, 0, numread);
} while (true);
Log.i(TAG, "getDataSource() Download ok...");
dialog.cancel();
dialog.dismiss();
openFile(myTempFile);
try {
更多精彩
赞助商链接