/** 升级成功,更新升级日期和版本号,和版本code */ private void alearyUpdateSuccess() { SharedPreferences sharedPreference = mContext.getSharedPreferences(SETTING_UPDATE_APK_INFO, 0); sharedPreference .edit() .putString(UPDATE_DATE, sdf.format(new Date())) .putString(APK_VERSION, apkInfo.getApkVersion()) .putInt(APK_VERCODE, apkInfo.getApkCode()) .commit(); }
public UpdateAsyncTask(Context mContext, UpdateApkInfo apkInfo) { this.mContext = mContext; this.apkInfo = apkInfo; if (apkInfo != null) { fileName = savePath + "/" + apkInfo.getApkName(); } }
@Override protected String doInBackground(Integer... params) { String result = ""; if (apkInfo == null) { result = "fail"; } else if (apkInfo != null && sdExists) { InputStream is = null; FileOutputStream fos = null; File file = new File(savePath); if (!file.exists()) { file.mkdirs(); } try { URL url = new URL(apkInfo.getDownloadUrl()); URLConnection urlConn = url.openConnection(); is = urlConn.getInputStream(); int length = urlConn.getContentLength(); // 文件大小 fos = new FileOutputStream(fileName); int count = 0, numread = 0; byte buf[] = new byte[1024]; while (!interceptFlag && (numread = is.read(buf)) != -1) { count += numread; int progressCount = (int) (((float) count / length) * 100); publishProgress(progressCount); fos.write(buf, 0, numread); } fos.flush(); result = "success"; } catch (Exception e) { e.printStackTrace(); result = "fail"; } finally { try { if (fos != null) fos.close(); if (is != null) is.close(); } catch (IOException e) { e.printStackTrace(); result = "fail"; } } } return result; }