android自动检测版本及自动升级源码,Android自动检测版本及自动升级

该代码实现了一个检查版本任务,从服务器获取XML解析并比较版本号。如果版本不同,则显示更新对话框,用户可以选择下载新版本的APK进行升级;如果版本相同,则直接进入程序主界面。下载过程通过ProgressDialog展示进度,并在完成后安装APK。
摘要由CSDN通过智能技术生成

/*

* 从服务器获取xml解析并进行比对版本号

*/

public class CheckVersionTask implements Runnable{

public void run() {

try {

//从资源文件获取服务器 地址

String path = getResources().getString(R.string.serverurl);

//包装成url的对象

URL url = new URL(path);

HttpURLConnection conn =  (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(5000);

InputStream is =conn.getInputStream();

info =  UpdataInfoParser.getUpdataInfo(is);

if(info.getVersion().equals(versionname)){

Log.i(TAG,"版本号相同无需升级");

LoginMain();

}else{

Log.i(TAG,"版本号不同 ,提示用户升级 ");

Message msg = new Message();

msg.what = UPDATA_CLIENT;

handler.sendMessage(msg);

}

} catch (Exception e) {

// 待处理

Message msg = new Message();

msg.what = GET_UNDATAINFO_ERROR;

handler.sendMessage(msg);

e.printStackTrace();

}

}

}

Handler handler = new Handler(){

@Override

public void handleMessage(Message msg) {

// TODO Auto-generated method stub

super.handleMessage(msg);

switch (msg.what) {

case UPDATA_CLIENT:

//对话框通知用户升级程序

showUpdataDialog();

break;

case GET_UNDATAINFO_ERROR:

//服务器超时

Toast.makeText(getApplicationContext(), "获取服务器更新信息失败", 1).show();

LoginMain();

break;

case DOWN_ERROR:

//下载apk失败

Toast.makeText(getApplicationContext(), "下载新版本失败", 1).show();

LoginMain();

break;

}

}

};

/*

*

* 弹出对话框通知用户更新程序

*

* 弹出对话框的步骤:

*  1.创建alertDialog的builder.

*  2.要给builder设置属性, 对话框的内容,样式,按钮

*  3.通过builder 创建一个对话框

*  4.对话框show()出来

*/

protected void showUpdataDialog() {

AlertDialog.Builder builer = new Builder(this) ;

builer.setTitle("版本升级");

builer.setMessage(info.getDescription());

//当点确定按钮时从服务器上下载 新的apk 然后安装

builer.setPositiveButton("确定", new OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

Log.i(TAG,"下载apk,更新");

downLoadApk();

}

});

//当点取消按钮时进行登录

builer.setNegativeButton("取消", new OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

LoginMain();

}

});

AlertDialog dialog = builer.create();

dialog.show();

}

/*

* 从服务器中下载APK

*/

protected void downLoadApk() {

final ProgressDialog pd;    //进度条对话框

pd = new  ProgressDialog(this);

pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

pd.setMessage("正在下载更新");

pd.show();

new Thread(){

@Override

public void run() {

try {

File file = DownLoadManager.getFileFromServer(info.getUrl(), pd);

sleep(3000);

installApk(file);

pd.dismiss(); //结束掉进度条对话框

} catch (Exception e) {

Message msg = new Message();

msg.what = DOWN_ERROR;

handler.sendMessage(msg);

e.printStackTrace();

}

}}.start();

}

//安装apk

protected void installApk(File file) {

Intent intent = new Intent();

//执行动作

intent.setAction(Intent.ACTION_VIEW);

//执行的数据类型

intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");

startActivity(intent);

}

/*

* 进入程序的主界面

*/

private void LoginMain(){

Intent intent = new Intent(this,MainActivity.class);

startActivity(intent);

//结束掉当前的activity

this.finish();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值