Android apk版本检测及更新

很多Android应用都具有版本检测自动更新功能,用户一键就可以完成软件的升级更新。这得益于Android系统的软件包管理和安装机制,这一功能实现起来也很简单。
 
Android的apk版本信息的获取:
定义在androidmanifest.xml里
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.huazi.updateapksamples"
      android:versionCode="1"
      android:versionName=" 1.0.0">
 
      <application ></application>
</manifest>
 
注意红色加粗字段,这个就是版本信息,其中versionCode常用来新旧版本的比较。
至于怎么在code里获取版本信息呢?
方法如下:
    PackageInfo info = getPackageManager().getPackageInfo("com.huazi.mylive", 0);
    String versionName = info.versionName;
int versionCode = info.versionCode;   
 
关键源码说明:
1、 ver.json 用于服务器上软件版本检测
[{"appname":"updateapksamples","apkname":"updateapksamples.apk","verName":1.0.1,"verCode":2}] 
 
2、服务器上版本获取,也即解析json
private boolean getServerVerCode() {
              try {
                     String verjson = NetworkTool.getContent(Config.UPDATE_SERVER
                                   + Config.UPDATE_VERJSON);
                     JSONArray array = new JSONArray(verjson);
                     if (array.length() > 0) {
                            JSONObject obj = array.getJSONObject(0);
                            try {
                                   newVerCode = Integer.parseInt(obj.getString("verCode"));
                                   newVerName = obj.getString("verName");
                            } catch (Exception e) {
                                   newVerCode = -1;
                                   newVerName = "";
                                   return false;
                            }
                     }
              } catch (Exception e) {
                     Log.e(TAG, e.getMessage());
                     return false;
              }
              return true;
       }
 
3、当前软件版本获取
       public static int getVerCode(Context context) {
              int verCode = -1;
              try {
                     verCode = context.getPackageManager().getPackageInfo(
                                   "com.huazi.updateapksamples", 0).versionCode;
              } catch (NameNotFoundException e) {
                     Log.e(TAG, e.getMessage());
              }
              return verCode;
       }
 
4. 下载模块
public class NetworkTool {
      
      
       public static String getContent(String url) throws Exception{
           StringBuilder sb = new StringBuilder();
          
           HttpClient client = new DefaultHttpClient();
           HttpParams httpParams = client.getParams();
           //设置网络超时参数
           HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
           HttpConnectionParams.setSoTimeout(httpParams, 5000);
           HttpResponse response = client.execute(new HttpGet(url));
           HttpEntity entity = response.getEntity();
            if (entity != null) {BufferedReader reader = new BufferedReader(
new InputStreamReader(entity.getContent(), "UTF-8"), 8192);
              
               String line = null;
               while ((line = reader.readLine())!= null){
                   sb.append(line + "\n");
               }
               reader.close();
           }
           return sb.toString();
       }
}
 
下面,进行版本检测更新的测试!
 
1.修改andriodmanifest.xml
  android:versionCode="2"
  android:versionName="1.0.1"  
 修改string.xml
 <string name="hello">Hello World, UpdateActivity! new !</string>
 编译出apk到d:/myapp/ updateapksamples.apk
 
2.在d:/myapp/下建立ver.json,内容如下:
  [{"appname":"updateapksamples","apkname":"updateapksamples.apk","verName":1.0.1,"verCode":2}]
 
3.为d:/myapp配置一个tomcat的工作目录
方法如下:tomcat目录/conf/ server.xml的host字段增加红色部分
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" >
            <Context path="/mp3" docBase="D:/mp3"></Context>
            <Context path="/myapp" docBase="D:/myapp"></Context>    
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->
 
        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
               
      </Host>
 
测试配置结果:


Android <wbr>apk版本检测及更新
 
4. 修改andriodmanifest.xml
  android:versionCode="1"
  android:versionName="1.0.0"  
 修改string.xml
 <string name="hello">Hello World, UpdateActivity! </string>
 模拟器上运行:(运行结果)
 Android <wbr>apk版本检测及更新
 
  弹出对话框,说明软件版本检测成功!
 
5. 选择更新,在下载成功后,会提示replace application,选择OK,安装新版本apk,
然后运行新版本的apk,如下:
 
Android <wbr>apk版本检测及更新 
 
可以看到显示的字串后面多了内容 new!
版本内容也是服务器上的apk的版本信息,说明软件更新成功!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值