Android APP应用都有版本的更新,那么怎样实现更新呢?今天就为大家介绍应用自动更新的全过程。
程序自动更新的流程大致如下:
程序启动 -->适时后台检查更新--->链接远程服务器-->获取新版本信息
-->比对当前版本-->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; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.check); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { checkVersion(); } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.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() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }).create(); alertDialog.show(); } } 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; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.check); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { checkVersion(); } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.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() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public 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; } } 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; /** * 更新信息实体类 * 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; } } </span> 获取当前已安装版本信息: 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; } } 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; } } 其实整个过程并不是很麻烦,需要注释的地方都已详细注释了,就写那么多。有问题请留言大家一起交流学习!