1.获取当前客户端版本信息
<span style="white-space:pre"> </span>/**
* 获取当前客户端版本信息
*/
private void getCurrentVersion(){
try {
PackageInfo info = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
curVersionName = info.versionName;
curVersionCode = info.versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace(System.err);
}
}
2..获取服务端版本信息
/*
* 检查版本更新
*/
public static UpdataInfo getUpdataInfo(InputStream is) throws Exception{
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "utf-8");//设置解析的数据源
int type = parser.getEventType();
UpdataInfo info = new UpdataInfo();//实体
while(type != XmlPullParser.END_DOCUMENT ){
switch (type) {
case XmlPullParser.START_TAG:
if("versionCode".equals(parser.getName())){
info.setVersionCode(parser.nextText()); //获取版本名称
}else if ("versionName".equals(parser.getName())){
info.setName(parser.nextText()); //获取版本号
}else if ("url".equals(parser.getName())){
info.setUrl(parser.nextText()); //获取要升级的APK文件
}else if ("updateLog".equals(parser.getName())){
info.setUpdateLog(parser.nextText()); //获取该文件的信息
}
break;
}
type = parser.next();
}
return info;
}
3.版本判断,下载更新
if(mUpdate != null){
if(curVersionCode < mUpdate.getVersionCode()){
<span style="white-space:pre"> </span>apkUrl = mUpdate.getDownloadUrl();
<span style="white-space:pre"> </span>updateMsg = mUpdate.getUpdateLog();
<span style="white-space:pre"> </span>showNoticeDialog();
}else if(isShowMsg){
<span style="white-space:pre"> </span>showLatestOrFailDialog(DIALOG_TYPE_LATEST);
}
}
<span style="white-space:pre"> </span>/**
* 显示版本更新通知对话框
*/
private void showNoticeDialog(){
AlertDialog.Builder builder = new Builder(mContext);
builder.setTitle("软件版本更新");
builder.setMessage(updateMsg);
builder.setPositiveButton("立即更新", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
showDownloadDialog();
}
});
builder.setNegativeButton("以后再说", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
noticeDialog = builder.create();
noticeDialog.show();
}
/**
* 显示下载对话框
*/
private void showDownloadDialog(){
AlertDialog.Builder builder = new Builder(mContext);
builder.setTitle("正在下载新版本");
final LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.update_progress, null);
mProgress = (ProgressBar)v.findViewById(R.id.update_progress);
mProgressText = (TextView) v.findViewById(R.id.update_progress_text);
builder.setView(v);
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
interceptFlag = true;
}
});
builder.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
dialog.dismiss();
interceptFlag = true;
}
});
downloadDialog = builder.create();
downloadDialog.setCanceledOnTouchOutside(false);
downloadDialog.show();
downloadApk();
}
<span style="white-space:pre"> </span>/**
* 下载apk
*/
private void downloadApk(){
downLoadThread = new Thread(mdownApkRunnable);
downLoadThread.start();
}
<span style="white-space:pre"> </span>/**
* 安装apk
*/
private void installApk(){
File apkfile = new File(apkFilePath);
if (!apkfile.exists()) {
return;
}
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
mContext.startActivity(i);
}
下载线程代码
private Runnable mdownApkRunnable = new Runnable() {
@Override
public void run() {
try {
String apkName = "OSChinaApp_"+mUpdate.getVersionName()+".apk";
String tmpApk = "OSChinaApp_"+mUpdate.getVersionName()+".tmp";
//判断是否挂载了SD卡
String storageState = Environment.getExternalStorageState();
if(storageState.equals(Environment.MEDIA_MOUNTED)){
savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/OSChina/Update/";
File file = new File(savePath);
if(!file.exists()){
file.mkdirs();
}
apkFilePath = savePath + apkName;
tmpFilePath = savePath + tmpApk;
}
//没有挂载SD卡,无法下载文件
if(apkFilePath == null || apkFilePath == ""){
mHandler.sendEmptyMessage(DOWN_NOSDCARD);
return;
}
File ApkFile = new File(apkFilePath);
//是否已下载更新文件
if(ApkFile.exists()){
downloadDialog.dismiss();
installApk();
return;
}
//输出临时下载文件
File tmpFile = new File(tmpFilePath);
FileOutputStream fos = new FileOutputStream(tmpFile);
URL url = new URL(apkUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
//显示文件大小格式:2个小数点显示
DecimalFormat df = new DecimalFormat("0.00");
//进度条下面显示的总文件大小
apkFileSize = df.format((float) length / 1024 / 1024) + "MB";
int count = 0;
byte buf[] = new byte[1024];
do{
int numread = is.read(buf);
count += numread;
//进度条下面显示的当前下载文件大小
tmpFileSize = df.format((float) count / 1024 / 1024) + "MB";
//当前进度值
progress =(int)(((float)count / length) * 100);
//更新进度
mHandler.sendEmptyMessage(DOWN_UPDATE);
if(numread <= 0){
//下载完成 - 将临时下载文件转成APK文件
if(tmpFile.renameTo(ApkFile)){
//通知安装
mHandler.sendEmptyMessage(DOWN_OVER);
}
break;
}
fos.write(buf,0,numread);
}while(!interceptFlag);//点击取消就停止下载
fos.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
};