在做项目2期有个功能是不可忽视的,那就是对老版本的迭代。
今天和大家分享下在线下载APK并进行安装,大致的步骤是这样的:
1.先从服务器中获得版本信息,如果不匹配则进行第2部.
2.在线下载APK,优先选择sd卡下载;
3.下载完成后调用隐形intent进行安装。
1.获得当前的版本号:
public int getAppVersionCode() {
PackageManager manager = this.getPackageManager();
PackageInfo info;
try {
info = manager.getPackageInfo(this.getPackageName(), 0);
// 当前版本的版本号
int versionCode = Integer.valueOf(info.versionName.replace(".", "0"));
Log.i(TAG, "<getAppVersionCode> -- versionCode = " + versionCode);
return versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
return 0;
}
}
2.判断当前版本号,如果是老版的则进行下载:
if (getAppVersionCode() < appVersionCode) {
// 弹出对话框通知用户更新程序
runOnUiThread(new Runnable() {
public void run() {
downLoadApk();
}
});
// showUpdataDialog();
}
3.下载并安装APK:
// 从服务器中下载APK
protected void downLoadApk() {
final ProgressDialog pd; // 进度条对话框
pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage(getString(R.string.version_updating));
pd.setCanceledOnTouchOutside(false);
pd.show();
new Thread() {
@Override
public void run() {
try {
File file = FileUtil.getFileFromServer(mContext, mAppDownloadUrl, pd, Constant.APK_NAME);
sleep(3000);
installApk(file);
pd.dismiss(); // 结束掉进度条对话框
} catch (Exception e) {
// showToast(mContext, getString(R.string.version_update_error), Toast.LENGTH_SHORT);
pd.dismiss();
e.printStackTrace();
}
}
}.start();
}
// 安装apk
protected void installApk(File file) {
if (file == null) {
// showToast(mContext, getString(R.string.version_update_error), Toast.LENGTH_SHORT);
return;
}
Intent intent = new Intent();
// 执行动作
intent.setAction(Intent.ACTION_VIEW);
// 执行的数据类型
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);
}
4.下载的具体方法:
// 从服务器下载apk
public static File getFileFromServer(Context context, String urlPath, ProgressDialog pd, String apkName) throws Exception {
// 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
// 获取到文件的大小
pd.setMax(conn.getContentLength());
InputStream is = conn.getInputStream();
File file = null;
File apkFile = null;
if (hasSDCard()) {
file = new File(getRootFilePath());
// File file = new File("/storage/sdcard1/");
if (!file.exists()) {
file.mkdir();
}
apkFile = new File(getRootFilePath() + apkName);
} else {
file = new File("/storage/sdcard0/Android/data/com.kmfex.smartfex/");// com.kmfex.smartfex
apkFile = new File("/storage/sdcard0/Android/data/com.kmfex.smartfex/" + apkName);
if (!file.exists()) {
if (file.mkdir()) {
// apkFile = new File("/storage/sdcard0/" + apkName);
} else {
file = new File("/storage/sdcard1/Android/data/com.kmfex.smartfex/");
apkFile = new File("/storage/sdcard1/Android/data/com.kmfex.smartfex/" + apkName);
if (!file.exists()) {
if (file.mkdir()) {
// apkFile = new File("/storage/sdcard1/" + apkName);
} else {
file = new File("/storage/sdcard/Android/data/com.kmfex.smartfex/");
if (!file.exists()) {
// file.mkdir();
// fail:alert"请插入SD卡"
Toast.makeText(context, "请插入SD卡", Toast.LENGTH_LONG).show();
}
apkFile = new File("/storage/sdcard/Android/data/com.kmfex.smartfex/" + apkName);
}
}
}
}
}
// Log.i("FileUtils", "<getFileFromServer> -- file = " + file);
FileOutputStream fos = new FileOutputStream(apkFile);
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;
// 获取当前下载量
pd.setProgress(total);
}
fos.close();
bis.close();
is.close();
return apkFile;
}
public static boolean hasSDCard() {
String status = Environment.getExternalStorageState();
if (!status.equals(Environment.MEDIA_MOUNTED)) {
return false;
}
return true;
}
public static String getRootFilePath() {
// if (hasSDCard()) {
return Environment.getExternalStorageDirectory().getAbsolutePath() + "/";// filePath:/sdcard/
// } else {
// // String dirPath =
// return Environment.getDataDirectory().getAbsolutePath(); // filePath:
// // /data/data/
// }
}