Android软件更新
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软件更新的更多相关文章
- Android软件更新安装。
app的开发有一个问题是避免不了的,那就是软件的升级维护. 这里我在查过一些资料和写了一个升级帮助类.使用很方便.直接导入就可以了. ( VersionBean.class为更新地址返回的数据对象,我 ...
- 实例源码--Android软件更新模块
下载源码 技术要点: (1) 通过网络检测服务器版本与本地版本 (2) 通过服务器下载最新版本 (3) 自动覆盖安装本地版本 详细介绍: 主要源码实现如下:
- android自动更新软件版本
根据网上的然后小改 import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import j ...
- Android - 软件自动更新的实现
转自:http://blog.csdn.net/wwj_748/article/details/8195565 接触到一个很实用的技术,那就是软件自动更新.一般开发者是通过自行在应用平台添加更新版本的 ...
- Android - 软件自动更新的实现(转)
在客户端实现更新操作 涉及到三个技术: 1.xml文件的解析 2.HttpURLConnection连接 3.文件流I/O 这里创建一个解析xml文件的服务类:ParXmlService.java p ...
- android自动更新程序,安装完以后就什么都没有了,没有出现安装成功的界面的问题
转载自: http://blog.csdn.net/lovexieyuan520/article/details/9250099 在android软件开发中,总是需要更新版本,所以当有新版本开发的时候 ...
- 安卓升级提示 phoneGap APK软件更新提示
以下代码由PHP200 阿杜整理 package com.example.syzx; import java.io.BufferedReader; import java.io.File; imp ...
- Android SDK 更新时修改hosts文件仍然无法更新,可试试这个方法……
Android SDK 更新时修改hosts文件仍然无法更新,此时必定万分蛋疼.在hosts文件中更换了各种ip,仍然解决不了!!!!!!!!!!!!!!? 第一步: 打开此软件,等待服务器连接 第二 ...
- Android 自动更新 + IIS7 添加APK mime
如果APK文件放在IIS下面需要添加APK的mime,否则会出现下面错误 可以在IIS上添加mime映射 .apk application/vnd.android 下面内容转自:http://ww ...
随机推荐
- Hibernate利用@DynamicInsert和@DynamicUpdate生成动态SQL语句
最近在使用Hibernate4中,发现两个很有奥秘的注解 @DynamicInsert 和 @DynamicUpdate 如果是在配置文件的话那就是dynamic -insert 和 dynamic- ...
- PHP去调用jenkins
背景说明:目前公司用到 jenkins(持续集成开源工具)更多情况下,通过配置,可以在gitlab上通过webhooks去调度jenkins 或者通过 jenkins管理后台,去手动执行“立即构建”去 ...
- poj 3678(SCC+2-SAT)
传送门:Problem 3678 https://www.cnblogs.com/violet-acmer/p/9769406.html 难点: 题意理解+构图 题意: 有n个点 v[0,2..... ...
- 7.STL
一.什么是STL STL是standart template library,标准模板库. 是HP实验室开发的一系列软件的统称,从根本上来说,它是一些容器和算法的集合,它是世界上很多最聪明的程序员 ...
- jackson用法
ObjectMapper mapper=new ObjectMapper(); //3.调用mapper的writeValueAsString()方法把一个对象或集合转为json字符串 Custome ...
- Linux将ActiveMQ修改为自启动
1.在root权限下操作 2.ln -s /usr/local/activemq/bin/activemq /etc/init.d/activemq 3.vi /etc/init.d/activem ...
- springboot下实现邮件发送功能
springboot给我们封装好了邮件功能,非常简单,只需要稍微配置下就ok. 引入jar <dependency> <groupId>org.springframework. ...
- CentOS下查找java环境变量
CentOS下通过命令查找java环境变量 [root@yuteng opt]# echo $JAVA_HOME /usr/java/default [root@yuteng opt]# which ...
- 解决zabbix可用性为灰色状态
zabbix添加主机后,获取不到zabbix agent状态,并且图标为灰色,而在zabbix-server端使用zabbix_get可以正常获取到数据.在正常情况下应为绿色或红色. 问题原因,添加主 ...
- 20155209 2016-2017-2 《Java程序设计》第九周学习总结 ## 教材学习内容总结
教材学习内容总结 JDBC API 允许用户访问任何形式的表格数据,尤其是存储在关系数据库中的数据. 执行流程: •连接数据源,如:数据库. •为数据库传递查询和更新指令. •处理数据库响应并返回的结 ...