目录(?)[-]
versionCode 1//对消费者不可见,仅用于应用市场、程序内部识别版本,判断新旧等用途。
versionName "1.0"//展示给消费者,消费者会通过它认知自己安装的版本.
//更新版本修改versionCode的值,必须是int哦
//版本升级apk的地址
private final String path = "http://www.zgylzb.com/.../update.asp";
/**
* 获取升级信息
* @return
* @throws Exception
*/
public UpdateInfo getUpDateInfo() throws Exception {
StringBuilder sb = new StringBuilder();
String line;
BufferedReader reader = null;
UpdateInfo updateInfo;
try {
// 创建一个url对象
URL url = new URL(path);
// 通過url对象,创建一个HttpURLConnection对象(连接)
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(3000);
urlConnection.setReadTimeout(3000);
if (urlConnection.getResponseCode() == 200) {
// 通过HttpURLConnection对象,得到InputStream
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
while ((line = reader.readLine()) != null) {
sb.append(line);
}
String info = sb.toString();
//对升级的信息进行封装
updateInfo = new UpdateInfo();
//版本
updateInfo.setVersion(info.split("&")[1]);
//升级说明
updateInfo.setDescription(info.split("&")[2]);
//apk下载地址
updateInfo.setUrl(info.split("&")[3]);
return updateInfo;
}
} catch (UnknownHostException e) {
return null;//连接超时
} catch (Exception e) {
e.printStackTrace();//网络请求出错
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
if (isNeedUpdate()) {
showUpdateDialog(); //需要更新版本
}
/**
* 判断apk是否需要升级
*
* @return true需要| false不需要
*/
private boolean isNeedUpdate() {
// 最新版本的版本号,info就是上面封装了更新日志信息的对象
int v = Integer.parseInt(info.getVersion());
if (server > getVersion())) {
//需要升级
return true;
} else {
//不需要升级,直接启动启动Activity
return false;
}
}
//获取当前版本号
private int getVersion() {
try {
PackageManager packageManager = getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
getPackageName(), 0);
return packageInfo.versionCode;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
/**
* 显示升级信息的对话框
*/
private void showUpdateDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(WelcomeActivity.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("请升级APP版本至" + info.getVersion());
builder.setMessage(info.getDescription());
builder.setCancelable(false);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
downFile(info.getUrl());//点击确定将apk下载
} else {
Toast.makeText(WelcomeActivity.this, "SD卡不可用,请插入SD卡", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//用户点击了取消
}
});
builder.create().show();
}
/**
* 下载最新版本的apk
*
* @param path apk下载地址
*/
private void downFile(final String path) {
pBar = new ProgressDialog(WelcomeActivity.this);
pBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pBar.setCancelable(false);
pBar.setTitle("正在下载...");
pBar.setMessage("请稍候...");
pBar.setProgress(0);
pBar.show();
new Thread() {
public void run() {
try {
URL url = new URL(path);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(5000);
con.setConnectTimeout(5000);
con.setRequestProperty("Charset", "UTF-8");
con.setRequestMethod("GET");
if (con.getResponseCode() == 200) {
int length = con.getContentLength();// 获取文件大小
InputStream is = con.getInputStream();
pBar.setMax(length); // 设置进度条的总长度
FileOutputStream fileOutputStream = null;
if (is != null) {
//对apk进行保存
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath(), "home.apk");
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch;
int process = 0;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
process += ch;
pBar.setProgress(process); // 实时更新进度了
}
}
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
//apk下载完成,使用Handler()通知安装apk
handler.sendEmptyMessage(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
//将下载进度对话框取消
pBar.cancel();
//安装apk,也可以进行静默安装
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "home.apk")),
"application/vnd.android.package-archive");
startActivityForResult(intent, 10);