android apk在线升级

 

现在android开发,一般采用的是CS模式,那么apk的升级,自然而然需要有server端的支持。一般,我们将升级版本以及一个记录升级版本的配置文件(在本文中采用jsonarray格式)放在server端。当Client初始化时,如果检测到server端有更新的版本(读取server的配置文件),则将在server端的升级版本以Http的方式连接,将其下载下来,然后调用android的api进行替换升级。

一、配置文件:
update_version.json
[{"appname":"myapp","apkname":"myapp.apk","verName":1.0,"verCode":1}]
版本的比较,是根据update_version.json中的verCode 以及apk文件中AndroidManifest.xml文件中的定义的版本号:
android:versionCode="1" android:versionName="1.0" 
二、初始化时比较版本
//比较服务器版本
//在 onCreate函数中调用
if (getServerVerCode()) {
int vercode = Config.getVerCode(this);
if (newVerCode > vercode) {
doNewVersionUpdate();//发现新版本更新 
} else {
Toast.makeText(getApplicationContext(),"目前是最新版本,感谢您的支持",Toast.LENGTH_LONG).show();//没有新版本 
}
}
//子函数,获取版本号 
private boolean getServerVerCode() { 
try { 
//取得服务器地址和接口文件名
String verjson = NetworkTool.getContent(Config.UPDATE_SERVER + Config.UPDATE_VERJSON);

JSONArray array = new JSONArray(verjson);

if (array.length() > 0) { JSONObject obj = array.getJSONObject(0);
try {
newVerCode = Integer.parseInt(obj.getString("verCode")); 
newVerName = obj.getString("verName");
} catch (Exception e) {
newVerCode = -1; 
newVerName = "";
return false;


} catch (Exception e) { 
Log.e(TAG, e.getMessage()); 
return false; 

return true; 

//子函数,若是有新版本,则 
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[1024]; 
int ch = -1; 
int count = 0; 
while ((ch = is.read(buf)) != -1) { 
fileOutputStream.write(buf, 0, ch); 
count += ch;
if (length > 0) { } 


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); 
}

我的话费充值店-各种面额

电信100元仅售98.60 
联通100仅售99.00
移动100仅售99.30

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值