package update.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.Xml;
public class UpdateManager {
private String curVersion;//当前版本名
private String newVersion;//新版本名
private int curVersionCode;//当前版本号
private int newVersionCode;//新版本号
private String updateInfo;
private UpdateCallback callback;//返回
private Context ctx;
private int progress;
private Boolean hasNewVersion;
private Boolean canceled;//取消
public static final String UPDATE_XMLURL = "http://192.168.101.128/updateversion/versions.xml";
public static final String UPDATE_APKNAME = "com.drkj.updata.apk";
private static final int UPDATE_CHECKCOMPLETED = 1;//检查
private static final int UPDATE_DOWNLOADING = 2; //正在下载
private static final int UPDATE_DOWNLOAD_ERROR = 3; //下载失败
private static final int UPDATE_DOWNLOAD_COMPLETED = 4; //下载完成
private static final int UPDATE_DOWNLOAD_CANCELED = 5;//取消
updateInfo info;
public UpdateManager(Context context, UpdateCallback updateCallback) {
ctx = context;
callback = updateCallback;
canceled = false;
getCurVersion();
}
public String getNewVersionName()
{
return newVersion;
}
public String getUpdateInfo()
{
return updateInfo;
}
private void getCurVersion() {
try {
PackageInfo pInfo = ctx.getPackageManager().getPackageInfo(
ctx.getPackageName(), 0);
curVersion = pInfo.versionName;
curVersionCode = pInfo.versionCode;
} catch (NameNotFoundException e) {
Log.e("update", e.getMessage());
curVersion = "1.1.1000";
curVersionCode = 111000;
}
}
public void checkUpdate() {
hasNewVersion = false;
new Thread(){
/**
* @by wainiwann add
*
*/
@Override
public void run() {
try {
//从资源文件获取服务器 地址
String path = UPDATE_XMLURL;
//包装成url的对象
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//conn.setRequestProperty("content-type", "text/html");
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
InputStream is =null;
is = conn.getInputStream();
info = getUpdataInfo(is);
try {
newVersionCode = info.getVersion();
newVersion = info.getDescription();
updateInfo = "";
Log.i("newVerCode", newVersionCode
+ "新的版本本号");
Log.i("newVerName", newVersion
+ "新的版本名字");
// if(!newVersion.equals(curVersion)){
if(newVersionCode>curVersionCode){
hasNewVersion = true;
}
} catch (Exception e) {
newVersionCode = -1;
newVersion = "";
updateInfo = "";
}
} catch (Exception e) {
// 待处理
newVersionCode = -1;
newVersion = "";
updateInfo = "";
}
updateHandler.sendEmptyMessage(UPDATE_CHECKCOMPLETED);
};
}.start();
}
/*
* 用pull解析器解析服务器返回的xml文件 (xml封装了版本号)
*/
public static updateInfo getUpdataInfo(InputStream is) throws Exception{
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "utf-8");//设置解析的数据源
int type = parser.getEventType();
updateInfo info = new updateInfo();//实体
while(type != XmlPullParser.END_DOCUMENT ){
switch (type) {
case XmlPullParser.START_TAG:
if("version".equals(parser.getName())){
info.setVersion(Integer.parseInt(parser.nextText())); //获取版本号
}else if ("url".equals(parser.getName())){
info.setUrl(parser.nextText()); //获取要升级的APK文件
}else if ("description".equals(parser.getName())){
info.setDescription(parser.nextText()); //获取该文件的信息
}
break;
}
type = parser.next();
}
return info;
}
public void update() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.fromFile(new File(Environment.getExternalStorageDirectory(), UPDATE_APKNAME)),
"application/vnd.android.package-archive");
ctx.startActivity(intent);
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public void downloadPackage()
{
new Thread() {
@Override
public void run() {
try {
// if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//URL url = new URL(path);
URL url = new URL(info.getUrl());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
//获取到文件的大小
//pd.setMax(conn.getContentLength());
File ApkFile = new File(Environment.getExternalStorageDirectory(), UPDATE_APKNAME);
if(ApkFile.exists())
{
ApkFile.delete();
}
FileOutputStream fos = new FileOutputStream(ApkFile);
int count = 0;
byte buf[] = new byte[512];
do{
int numread = is.read(buf);
count += numread;
progress =(int)(((float)count / length) * 100);
updateHandler.sendMessage(updateHandler.obtainMessage(UPDATE_DOWNLOADING));
if(numread <= 0){
updateHandler.sendEmptyMessage(UPDATE_DOWNLOAD_COMPLETED);
break;
}
fos.write(buf,0,numread);
}while(!canceled);
if(canceled)
{
updateHandler.sendEmptyMessage(UPDATE_DOWNLOAD_CANCELED);
}
fos.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
updateHandler.sendMessage(updateHandler.obtainMessage(UPDATE_DOWNLOAD_ERROR,e.getMessage()));
} catch(IOException e){
e.printStackTrace();
updateHandler.sendMessage(updateHandler.obtainMessage(UPDATE_DOWNLOAD_ERROR,e.getMessage()));
}
}
}.start();
}
public void cancelDownload()
{
canceled = true;
}
Handler updateHandler = new Handler()
{
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_CHECKCOMPLETED:
callback.checkUpdateCompleted(hasNewVersion, newVersi
没有合适的资源?快使用搜索试试~ 我知道了~
android自动检测版本并更新下载,带进度条服务端解析xml
共49个文件
class:21个
java:8个
png:6个
需积分: 9 38 下载量 111 浏览量
2016-04-14
15:25:57
上传
评论 4
收藏 120KB RAR 举报
温馨提示
android检测版本自动更新并弹出对话框提示用户下载,带有进度条,服务端解析xml,已测过好多次,主要包名更签名!
资源推荐
资源详情
资源评论
收起资源包目录
解析xml自动更新版本.rar (49个子文件)
解析xml自动更新版本
服务端
updateversion
versions.xml 194B
Update_Test.apk 24KB
Update_Test
bin
classes.dex 21KB
res
crunch
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 2KB
dexedLibs
Update_Test.apk 24KB
classes
com
trinet
util
NetHelper.class 5KB
DialogHelper.class 4KB
update
test
UpdateManager$UpdateCallback.class 395B
BuildConfig.class 331B
Update_TestActivity.class 2KB
R$layout.class 373B
R$id.class 365B
R$drawable.class 379B
UpdateManager$2.class 3KB
UpdateManager$1.class 2KB
GetBroadcast.class 2KB
UpdateManager.class 6KB
updateInfo.class 937B
R$attr.class 319B
Update_TestActivity$2.class 833B
R.class 482B
Update_TestActivity$1$2.class 2KB
Update_TestActivity$1$1.class 1KB
R$string.class 950B
UpdateManager$3.class 3KB
Update_TestActivity$1.class 3KB
resources.ap_ 12KB
AndroidManifest.xml 2KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-xhdpi
values
strings.xml 912B
drawable-mdpi
icon.png 3KB
layout
main.xml 502B
assets
gen
update
test
BuildConfig.java 153B
R.java 2KB
proguard.cfg 1KB
.settings
org.eclipse.jdt.core.prefs 177B
src
com
trinet
util
DialogHelper.java 3KB
NetHelper.java 3KB
update
test
GetBroadcast.java 2KB
updateInfo.java 655B
UpdateManager.java 9KB
Update_TestActivity.java 4KB
.project 847B
.classpath 475B
project.properties 563B
AndroidManifest.xml 2KB
共 49 条
- 1
资源评论
BuLions
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 52193906181644561181711717834527.jpg
- 图片缓冲技术(android)源码
- 花数据集+数据集汇总+标签txt+数据集汇总代码+迁移学习最佳模型+全部迭代最佳模型
- 20240329224412.zip
- switch.docx `switch`语句是C++中的一种流程控制语句,通常用于根据表达式的值选择执行不同的代码块 下面是`
- python绘制直方图-02-进程之间不共享全局变量.ev4.rar
- python绘制直方图-01-第三天知识点回顾.ev4.rar
- 01背包问题动态规划.docx
- 表达式求值.docx表达式求值涉及许多不同的情况和方法,具体取决于表达式的形式和要求的精度 下面是一个简单的例子
- python绘制直方图-08-软件的卸载.ev4.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功