package com.beicai.update;
/**
* 版本更新信息的封装
*
* @author loupengfei
*
*/
public class UpdateEntity {
private String version;
private String url;
private String description;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
<pre name="code" class="java">package com.beicai.update;
import java.io.File;
import java.io.InputStream;
import org.xmlpull.v1.XmlPullParser;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Xml;
/**
*
* 获取服务器的更新内容
*
* 当前版本
*
* apk的路径
*
* 更新细则
*
* @author loupengfei
*
*/
public class UpdateTools {
/*
* 用pull解析器解析服务器返回的xml文件 (xml封装了版本号)
*/
public static UpdateEntity getUpdataInfo(InputStream is) throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "utf-8");// 设置解析的数据源
int type = parser.getEventType();
UpdateEntity info = new UpdateEntity();// 实体
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG:
if ("version".equals(parser.getName())) {
info.setVersion(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;
}
/**
*
* 安装Apk
*
* @param file
* @param context
*/
public void installApk(File file,Context context) {
Intent intent = new Intent();
//执行动作
intent.setAction(Intent.ACTION_VIEW);
//执行的数据类型
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
package com.beicai.update;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import com.baidu.mobstat.StatService;
//1,进入 请求服务器 xml文件
//2,读取xml文件信息 。取出版本号 ,apk的下载地址
//3,比较当前app的版本号和xml中版本号的大小,如果当前app《服务器版本号则下载 否则不弹出框
//4,如果要下载,则需要取出xml中的下载地址,然后下载apk ,存入到sd卡上
//5,安装下载好的apk
public class MainActivity extends Activity {
private AlertDialog dialog;
private UpdateEntity updateEntity;
//private TextView tv_name;
/**
* 消息机制
*/
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 0:
dialog.setMessage(updateEntity.getDescription());
dialog.show();
break;
case 1:
downLoadApk();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog = new AlertDialog.Builder(MainActivity.this).
setTitle("升级提醒").
setIcon(R.drawable.ic_launcher).
setPositiveButton("在线升级", onclick).
setNegativeButton("不想升级", null).
create();
/*tv_name = (TextView) findViewById(R.id.tv_update);
//单选按钮
tv_name.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,MainActivity1.class);
MainActivity.this.startActivity(intent);
}
});*/
//开启线程
new Thread(new CheckVersionTask()).start(); //检查版本号
}
DialogInterface.OnClickListener onclick = new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
handler.sendEmptyMessage(1);
}
};
/*
* 从服务器中下载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 = getFileFromServer(updateEntity.getUrl(), pd);
sleep(3000);
UpdateTools tools = new UpdateTools();
//安装APk
tools.installApk(file,MainActivity.this);
pd.dismiss(); //结束掉进度条对话框
} catch (Exception e) {
e.printStackTrace();
}
}}.start();
}
/*
* 从服务器获取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();
updateEntity = UpdateTools.getUpdataInfo(is);
//当前版本号
int versionCode = MainActivity.this.getPackageManager().getPackageInfo(MainActivity.this.getPackageName(), 0).versionCode;
if(Integer.parseInt(updateEntity.getVersion()) <= versionCode){
Log.i("xxx","版本号相同无需升级");
}else{
Log.i("xxxx","版本号不同 ,提示用户升级 ");
handler.sendEmptyMessage(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 下载方法
*
* @param path
* @param pd
* @return
* @throws Exception
*/
public File getFileFromServer(String path, ProgressDialog pd)
throws Exception {
// 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
// 获取到文件的大小
pd.setMax(conn.getContentLength());
InputStream is = conn.getInputStream();
File file = new File(Environment.getExternalStorageDirectory(),
"updata.apk");
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len;
int total = 0;
while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
total += len;
// 获取当前下载量
pd.setProgress(total);
}
fos.close();
bis.close();
is.close();
return file;
} else {
return null;
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
// StatService.onPause(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// StatService.onResume(this);
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
<!-- 版本更新 -->
<string name="serverurl">http://10.1.5.136:8080/versonupdateweb/v_file/update_file.xml</string>
///
<version>
<versioncode>2</versioncode>
<url>http://169.254.117.189:8080/yzw/apk/NewsMessage.apk</url>
</version>