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;
     @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解析工具代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<SPAN style= "FONT-SIZE: 10px" > 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;
     }
}
</SPAN>


updateInfo实体:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<SPAN style= "FONT-SIZE: 10px" > 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>


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<SPAN style= "FONT-SIZE: 10px" > 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;
     }
}
</SPAN>


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值