Android实现程序自动更新

来源:互联网 发布:php二次开发从哪里能学 编辑:程序博客网 时间:2024/04/29 17:14

http://www.android-study.com/wangluobiancheng/521.html

一个良好的Android程序都应该有版本的更新,那怎样实现更新呢?今天就为大家介绍应用自动更新的全过程。

程序自动更新的流程大致如下:

程序启动 -> 适时后台检查更新 -> 链接远程服务器 -> 获取新版本信息

-> 比对当前版本 -> if(有更新) -> 显示更新提示对话框并显示更新的内容 -> 交与用户选择

下面是我做的一个简单demo,大家可以参考一下:

布局比较简单就不上代码了。

主程序代码:

package com.cloay.update;import java.io.IOException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.pm.PackageManager.NameNotFoundException;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;/** * 程序自动更新 UpdateTestActivity.java *  * @author Cloay 2011-11-23 */public class UpdateTestActivity extends Activity {private Button button;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);button = (Button) findViewById(R.id.check);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {try {checkVersion();} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});}/** * 检查是否需要更新程序 *  * @throws NameNotFoundException */private void checkVersion() throws NameNotFoundException {UpdateInfo updateInfo = new UpdateInfo();URL url;try {url = new URL("http://localhost:8080/update.xml");HttpURLConnection connection = (HttpURLConnection) url.openConnection();// connection.setConnectTimeout(5000);updateInfo = ParseXmlUtils.parseXml(connection.getInputStream());} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}if (updateInfo.getVersion().equals(VersionUtil.getVersionName(this))) {Toast.makeText(this, "版本相同,不需要升级!", Toast.LENGTH_SHORT).show();} else {showUpdateDialog(updateInfo);}}private void showUpdateDialog(UpdateInfo updateInfo) {AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("提示检测到新版本,确定升级吗?").setIcon(R.drawable.ask).setMessage(updateInfo.getDescription()).setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.cancel();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.cancel();}}).create();alertDialog.show();}}

这里说明一下:远程服务器放置一个xml文件,用来说明当前新版本的信息。包括版本号,新版本功能说明,新版下载链接。

xml解析工具代码:

package com.cloay.update;import java.io.IOException;import java.io.InputStream;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;/** * 解析Xml文件 ParseXmlUtils.java *  * @author Cloay 2011-11-7 */public class ParseXmlUtils {/** * 通过InputStream 解析文件 *  * @param in * @return */public static UpdateInfo parseXml(InputStream in) {UpdateInfo updateInfo = new UpdateInfo();DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();DocumentBuilder db = null;try {db = dbf.newDocumentBuilder();Document doc = null;doc = db.parse(in);Element root = doc.getDocumentElement();NodeList resultNode = root.getElementsByTagName("info");for (int i = 0; i < resultnode.getlength();="" i++)="" {="" element="" res="(Element)" resultnode.item(i);="" updateinfo.setversion(res.getelementsbytagname("version")="" .item(0).getfirstchild().getnodevalue());="" updateinfo.seturl(res.getelementsbytagname("url").item(0)="" .getfirstchild().getnodevalue());="" updateinfo.setdescription(res="" .getelementsbytagname("description").item(0)="" .getfirstchild().getnodevalue());="" }="" }="" catch="" (parserconfigurationexception="" e)="" {="" e.printstacktrace();="" }="" catch="" (saxexception="" e)="" {="" e.printstacktrace();="" }="" catch="" (ioexception="" e)="" {="" e.printstacktrace();="" }="" return="" updateinfo;="" }="" }="">

updateInfo实体:

package com.cloay.update;/** * 更新信息实体类 UpdateInfo.java *  * @author Cloay 2011-11-23 */public class UpdateInfo {private String version; // 版本号private String url; // 新版本存放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;}}

获取当前已安装版本信息:

package com.cloay.update;import android.content.Context;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.content.pm.PackageManager.NameNotFoundException;/** * 版本工具类 VersionUtil.java *  * @author Cloay 2011-11-23 */public class VersionUtil {/** * 获取版本号 *  * @param context *            上下文 * @return * @throws NameNotFoundException */public static String getVersionName(Context context)throws NameNotFoundException {// 获取PackageManager 实例PackageManager packageManager = context.getPackageManager();// 获得context所属类的包名,0表示获取版本信息PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);return packageInfo.versionName;}}

整个过程并不麻烦,许多地方已经详细注释了,就写那么多。

0 0
原创粉丝点击