Android软件更新

//得到当前版本编码和版本名称、

public static int getVerCode(Context context) {
int verCode = -;
try {
verCode = context.getPackageManager().getPackageInfo("com.example", ).versionCode;
} catch (NameNotFoundException e) {
Log.e(TAG, e.getMessage());
}
return verCode;
}
public static String getVerName(Context context) {
String verName = "";
try {
verName = context.getPackageManager().getPackageInfo("com.example", ).versionName;
} catch (NameNotFoundException e) {
Log.e(TAG, e.getMessage());
}
return verName;
}
public static String getAppName(Context context) {
String verName = context.getResources().getText(R.string.app_name).toString();
return verName;
}

// 获取更新版本内容

public static String getContent(String url) throws Exception{
StringBuilder sb = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpParams httpParams = client.getParams();
//设置网络超时参数
HttpConnectionParams.setConnectionTimeout(httpParams, );
HttpConnectionParams.setSoTimeout(httpParams, );
HttpResponse response = client.execute(new HttpGet(url));
HttpEntity entity = response.getEntity();
if (entity != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"), );
String line = null;
while ((line = reader.readLine())!= null){
sb.append(line + "\n");
}
reader.close();
}
return sb.toString();
}
// 判断是否得到了服务器传来的版本号

private boolean getServerVerCode() {
try {
String verjson = NetworkTool.getContent(Config.UPDATE_SERVER + Config.UPDATE_VERJSON);
JSONArray array = new JSONArray(verjson);
if (array.length() > ) {
JSONObject obj = array.getJSONObject();
try {
newVerCode = Integer.parseInt(obj.getString("verCode"));
newVerName = obj.getString("verName");
} catch (Exception e) {
newVerCode = -;
newVerName = "";
return false;
}
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
return false;
}
return true;
}
// 如果版本号得到了就更新,否则不更新

if (getServerVerCode()) {
int vercode = Config.getVerCode(this);
if (newVerCode > vercode) {
doNewVersionUpdate();
} else {
notNewVersionShow();
}
}
// 更新软件的代码

private void doNewVersionUpdate() {
int verCode = Config.getVerCode(this);
String verName = Config.getVerName(this);
StringBuffer sb = new StringBuffer();
sb.append("当前版本:");
sb.append(verName);
sb.append(" Code:");
sb.append(verCode);
sb.append(", 发现新版本:");
sb.append(newVerName);
sb.append(" Code:");
sb.append(newVerCode);
sb.append(", 是否更新?");
Dialog dialog = new AlertDialog.Builder(UpdateActivity.this)
.setTitle("软件更新")
.setMessage(sb.toString())
// 设置内容
.setPositiveButton("更新",// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
pBar = new ProgressDialog(UpdateActivity.this);
pBar.setTitle("正在下载");
pBar.setMessage("请稍候...");
pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
downFile(Config.UPDATE_SERVER + Config.UPDATE_APKNAME);
}
})
.setNegativeButton("暂不更新",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int whichButton) {
// 点击"取消"按钮之后退出程序
finish();
}
}).create();// 创建
// 显示对话框
dialog.show();
} void downFile(final String url) {
pBar.show();
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is != null) {
File file = new File( Environment.getExternalStorageDirectory(),Config.UPDATE_SAVENAME);
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[];
int ch = -;
int count = ;
while ((ch = is.read(buf)) != -) {
fileOutputStream.write(buf, , ch);
count += ch;
if (length > ) {
}
}
}
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
down();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
} void down() {
handler.post(new Runnable() {
public void run() {
pBar.cancel();
update();
}
});
} void update() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), Config.UPDATE_SAVENAME)),
"application/vnd.android.package-archive");
startActivity(intent);
}
// 不更新软件的代码

private void notNewVersionShow() {
int verCode = Config.getVerCode(this);
String verName = Config.getVerName(this);
StringBuffer sb = new StringBuffer();
sb.append("当前版本:");
sb.append(verName);
sb.append(" Code:");
sb.append(verCode);
sb.append(",\n已是最新版,无需更新!");
Dialog dialog = new AlertDialog.Builder(UpdateActivity.this)
.setTitle("软件更新").setMessage(sb.toString())// 设置内容
.setPositiveButton("确定",// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
finish();
}
}).create();// 创建
// 显示对话框
dialog.show();
}

代码下载链接:http://www.apkbus.com/android-153953-1-1.html

Android软件更新的更多相关文章

  1. Android软件更新安装。

    app的开发有一个问题是避免不了的,那就是软件的升级维护. 这里我在查过一些资料和写了一个升级帮助类.使用很方便.直接导入就可以了. ( VersionBean.class为更新地址返回的数据对象,我 ...

  2. 实例源码--Android软件更新模块

    下载源码   技术要点: (1) 通过网络检测服务器版本与本地版本 (2) 通过服务器下载最新版本 (3) 自动覆盖安装本地版本 详细介绍: 主要源码实现如下:  

  3. android自动更新软件版本

    根据网上的然后小改 import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import j ...

  4. Android - 软件自动更新的实现

    转自:http://blog.csdn.net/wwj_748/article/details/8195565 接触到一个很实用的技术,那就是软件自动更新.一般开发者是通过自行在应用平台添加更新版本的 ...

  5. Android - 软件自动更新的实现(转)

    在客户端实现更新操作 涉及到三个技术: 1.xml文件的解析 2.HttpURLConnection连接 3.文件流I/O 这里创建一个解析xml文件的服务类:ParXmlService.java p ...

  6. android自动更新程序,安装完以后就什么都没有了,没有出现安装成功的界面的问题

    转载自: http://blog.csdn.net/lovexieyuan520/article/details/9250099 在android软件开发中,总是需要更新版本,所以当有新版本开发的时候 ...

  7. 安卓升级提示 phoneGap APK软件更新提示

    以下代码由PHP200 阿杜整理 package com.example.syzx;   import java.io.BufferedReader; import java.io.File; imp ...

  8. Android SDK 更新时修改hosts文件仍然无法更新,可试试这个方法……

    Android SDK 更新时修改hosts文件仍然无法更新,此时必定万分蛋疼.在hosts文件中更换了各种ip,仍然解决不了!!!!!!!!!!!!!!? 第一步: 打开此软件,等待服务器连接 第二 ...

  9. Android 自动更新 + IIS7 添加APK mime

    如果APK文件放在IIS下面需要添加APK的mime,否则会出现下面错误 可以在IIS上添加mime映射 .apk application/vnd.android   下面内容转自:http://ww ...

随机推荐

  1. 【UR #17】滑稽树前做游戏

    假装看懂的样子 假装会做的样子 UOJ Round #17 题解 加上一个(t-w)^c,c是和i相连的点的度数 是一个多项式的话可以归纳证明 一些具体实现: 多项式存储,保留t,y, f=ai*t^ ...

  2. 收藏:解决其它程序与IIS共享80端口的四个方法

    今天写的程序也占用80端口,而 IIS也占用 80端口,我在我的一张网卡上分配了两个IP地址,但是测试发现:只要IIS启动后,我写的程序就无法使用80端口,到网上搜索了一下,终于找到了解决办法: 使用 ...

  3. 关于连接linux被拒

    一.scp被拒 在服务器A命令行使用scp往服务器B传输,如: scp -i id_rsa_125 file1 user01@IP:/home #ssh_exchange_identification ...

  4. 读取Easy UI的DATa grid里面的所有数据

    目前我知道的有两种方法: 1.读取行数,循环读出 $("#btnEcxel").click(function () { var rows = $('#tbVehicleList') ...

  5. socket技术详解(看清socket编程)

    socket编程是网络常用的编程,我们通过在网络中创建socket关键字来实现网络间的通信,通过收集大量的资料,通过这一章节,充分的了解socket编程,文章用引用了大量大神的分析,加上自己的理解,做 ...

  6. Thread的setDaemon(true)方法

    定义:守护线程--也称“服务线程”,在没有用户线程可服务时会自动离开.优先级:守护线程的优先级比较低,用于为系统中的其它对象和线程提供服务. 设置:通过setDaemon(true)来设置线程为“守护 ...

  7. bzoj千题计划302:bzoj3160: 万径人踪灭

    https://www.lydsy.com/JudgeOnline/problem.php?id=3160 不连续的回文串数量=所有的回文序列数量-连续的回文子串 连续的回文子串: manacher ...

  8. CodeMirror 使用大全

    CodeMirror是一个用于编辑器文本框textarea代码高亮javascript插件,为各种编程语言实现关键字,函数,变量等代码高亮显示,丰富的api和可扩展功能以及多个主题样式,能满足您各种项 ...

  9. bootstrap modal垂直居中 (转)

    根据博友的经验,总结后请使用方法一就行了 一,修改bootstrap.js 源码 原来的: Modal.prototype.adjustDialog = function () { ].scrollH ...

  10. 查看Mac电脑的核心数量

    #显示物理核心数sysctl hw.physicalcpu#显示逻辑核心数sysctl hw.logicalcpu