现在的位置: 首页 > 综合 > 正文

android之程序自动更新的实现

2014年10月12日 ⁄ 综合 ⁄ 共 6857字 ⁄ 字号 评论关闭

应用都有版本的更新,那么怎样实现更新呢?今天就为大家介绍应用自动更新的全过程。

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

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

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


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

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

主程序代码:

[java] view
plain
copy

  1. package com.cloay.update;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.HttpURLConnection;  
  5. import java.net.MalformedURLException;  
  6. import java.net.URL;  
  7.   
  8. import android.app.Activity;  
  9. import android.app.AlertDialog;  
  10. import android.content.DialogInterface;  
  11. import android.content.pm.PackageManager.NameNotFoundException;  
  12. import android.os.Bundle;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16. import android.widget.Toast;  
  17. /** 
  18.  * 程序自动更新 
  19.  * UpdateTestActivity.java 
  20.  * @author Cloay 
  21.  * 2011-11-23 
  22.  */  
  23. public class UpdateTestActivity extends Activity {  
  24.     private Button button;  
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.           
  30.         button = (Button) findViewById(R.id.check);  
  31.         button.setOnClickListener(new OnClickListener() {  
  32.               
  33.             @Override  
  34.             public void onClick(View v) {  
  35.                 try {  
  36.                     checkVersion();  
  37.                 } catch (NameNotFoundException e) {  
  38.                     // TODO Auto-generated catch block  
  39.                     e.printStackTrace();  
  40.                 }  
  41.             }  
  42.         });  
  43.     }  
  44.       
  45.     /** 
  46.      * 检查是否需要更新程序 
  47.      * @throws NameNotFoundException  
  48.      */  
  49.     private void checkVersion() throws NameNotFoundException{  
  50.         UpdateInfo updateInfo = new UpdateInfo();  
  51.         URL url;  
  52.         try {  
  53.             url = new URL("http://localhost:8080/update.xml");  
  54.             HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
  55. //          connection.setConnectTimeout(5000);  
  56.             updateInfo = ParseXmlUtils.parseXml(connection.getInputStream());  
  57.               
  58.         } catch (MalformedURLException e) {  
  59.             e.printStackTrace();  
  60.         } catch (IOException e) {  
  61.             e.printStackTrace();  
  62.         }  
  63.         if(updateInfo.getVersion().equals(VersionUtil.getVersionName(this))){  
  64.             Toast.makeText(this"版本相同,不需要升级!", Toast.LENGTH_SHORT).show();  
  65.         }  
  66.         else{  
  67.             showUpdateDialog(updateInfo);  
  68.         }  
  69.     }  
  70.   
  71.     private void showUpdateDialog(UpdateInfo updateInfo) {  
  72.         AlertDialog alertDialog = new AlertDialog.Builder(this)  
  73.         .setTitle("提示检测到新版本,确定升级吗?")  
  74.         .setIcon(R.drawable.ask)  
  75.         .setMessage(updateInfo.getDescription())  
  76.         .setPositiveButton("确定",   
  77.         new DialogInterface.OnClickListener() {  
  78.             @Override  
  79.             public void onClick(DialogInterface dialog, int which) {  
  80.                 dialog.cancel();    
  81.             }  
  82.         })  
  83.         .setNegativeButton("取消",  
  84.         new DialogInterface.OnClickListener() {  
  85.               
  86.             @Override  
  87.             public void onClick(DialogInterface dialog, int which) {  
  88.                 dialog.cancel();      
  89.             }  
  90.         }).create();  
  91.         alertDialog.show();  
  92.     }  
  93. }  


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


xml解析工具代码:

[java] view
plain
copy

  1. <span style="font-size:10px;">package com.cloay.update;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5.   
  6. import javax.xml.parsers.DocumentBuilder;  
  7. import javax.xml.parsers.DocumentBuilderFactory;  
  8. import javax.xml.parsers.ParserConfigurationException;  
  9.   
  10. import org.w3c.dom.Document;  
  11. import org.w3c.dom.Element;  
  12. import org.w3c.dom.NodeList;  
  13. import org.xml.sax.SAXException;  
  14. /** 
  15.  * 解析Xml文件 
  16.  * ParseXmlUtils.java 
  17.  * @author Cloay 
  18.  * 2011-11-7 
  19.  */  
  20. public class ParseXmlUtils {  
  21.     /** 
  22.      * 通过InputStream 解析文件 
  23.      * @param in 
  24.      * @return 
  25.      */  
  26.     public static UpdateInfo parseXml(InputStream in){  
  27.         UpdateInfo updateInfo = new UpdateInfo();  
  28.         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();            
  29.         DocumentBuilder db = null;        
  30.         try {  
  31.             db = dbf.newDocumentBuilder();  
  32.             Document doc = null;      
  33.             doc = db.parse(in);   
  34.             Element root = doc.getDocumentElement();  
  35.             NodeList resultNode = root.getElementsByTagName("info");      
  36.             for(int i = 0; i < resultNode.getLength();i++){      
  37.                 Element res = (Element)resultNode.item(i);    
  38.                 updateInfo.setVersion(res.getElementsByTagName("version").item(0).getFirstChild().getNodeValue());  
  39.                 updateInfo.setUrl(res.getElementsByTagName("url").item(0).getFirstChild().getNodeValue());  
  40.                 updateInfo.setDescription(res.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());  
  41.             }  
  42.         } catch (ParserConfigurationException e) {  
  43.             e.printStackTrace();  
  44.         } catch (SAXException e) {  
  45.             e.printStackTrace();  
  46.         } catch (IOException e) {  
  47.             e.printStackTrace();  
  48.         }        
  49.         return updateInfo;  
  50.     }  
  51. }  
  52. </span>  


updateInfo实体:

[java] view
plain
copy

  1. <span style="font-size:10px;">package com.cloay.update;  
  2. /** 
  3.  * 更新信息实体类 
  4.  * UpdateInfo.java 
  5.  * @author Cloay 
  6.  * 2011-11-23 
  7.  */  
  8. public class UpdateInfo {    
  9.     private String version;    //版本号  
  10.     private String url;     //新版本存放url路径  
  11.     private String description;   //更新说明信息,比如新增什么功能特性等  
  12.     public String getVersion() {    
  13.         return version;    
  14.     }    
  15.     public void setVersion(String version) {    
  16.         this.version = version;    
  17.     }    
  18.     public String getUrl() {    
  19.         return url;    
  20.     }    
  21.     public void setUrl(String url) {    
  22.         this.url = url;    
  23.     }    
  24.     public String getDescription() {    
  25.         return description;    
  26.     }    
  27.     public void setDescription(String description) {    
  28.         this.description = description;    
  29.     }    
  30. }  </span>  


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

[java] view
plain
copy

  1. <span style="font-size:10px;">package com.cloay.update;  
  2.   
  3. import android.content.Context;  
  4. import android.content.pm.PackageInfo;  
  5. import android.content.pm.PackageManager;  
  6. import android.content.pm.PackageManager.NameNotFoundException;  
  7.   
  8. /** 
  9.  * 版本工具类 
  10.  * VersionUtil.java 
  11.  * @author Cloay 
  12.  * 2011-11-23 
  13.  */  
  14. public class VersionUtil {  
  15.     /** 
  16.      * 获取版本号 
  17.      * @param context 
  18.      *          上下文 
  19.      * @return 
  20.      * @throws NameNotFoundException  
  21.      */  
  22.     public static String getVersionName(Context context) throws NameNotFoundException{  
  23.         //获取PackageManager 实例  
  24.         PackageManager packageManager = context.getPackageManager();  
  25.         //获得context所属类的包名,0表示获取版本信息  
  26.         PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);  
  27.         return packageInfo.versionName;  
  28.     }  
  29. }  
  30. </span>  


其实整个过程并不是很麻烦,需要注释的地方都已详细注释了,就写那么多。有问题请留言大家一起交流学习!

抱歉!评论已关闭.