PhoneGap中文网

 找回密码
 立即注册
查看: 17107|回复: 1
打印 上一主题 下一主题

Android 应用程序自动更新源码 可用于phonegap

[复制链接]

493

主题

2035

帖子

6894

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
6894
跳转到指定楼层
楼主
发表于 2014-10-11 22:03:37 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
Android  应用程序自动更新源码 可用于phonegap



Android  应用程序自动更新源码 基本信息

源码分类:网络浏览
更新时间:2012-08-19
源码大小:0.13 MB
适用平台:Android2.2+


Android  应用程序自动更新源码简介
我们看到很多Android应用都具有自动更新功能,用户一键就可以完成软件的升级更新。得益于Android系统的软件包管理和安装机制,这一功能实现起来相当简单,下面我们就来实践一下。首先给出界面效果:
1.准备知识

<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
      package="com.myapp"  
      android:versionCode="1"  
      android:versionName="1.0.0">  
<application></application>  
</manifest
>

  其中,android:versionCode和android:versionName两个字段分别表示版本代码,版本名称。versionCode是整型数字,versionName是字符串。由于version是给用户看的,不太容易比较大小,升级检查时,可以以检查versionCode为主,方便比较出版本的前后大小。
那么,在应用中如何读取AndroidManifest.xml中的versionCode和versionName呢?可以使用PackageManager的API,参考以下代码:
public static int getVerCode(Context context) {   
        int verCode = -1;   
        try {   
            verCode = context.getPackageManager().getPackageInfo(   
                    "com.myapp", 0).versionCode;   
        } catch (NameNotFoundException e) {   
            Log.e(TAG, e.getMessage());   
        }   
        return verCode;   
    }   
      
    public static String getVerName(Context context) {   
        String verName = "";   
        try {   
            verName = context.getPackageManager().getPackageInfo(   
                    "com.myapp", 0).versionName;   
        } catch (NameNotFoundException e) {   
            Log.e(TAG, e.getMessage());   
        }   
        return verName;      
}

或者在AndroidManifest中将android:versionName="1.2.0"写成android:versionName="@string/app_versionName",然后在values/strings.xml中添加对应字符串,这样实现之后,就可以使用如下代码获得版本名称:
public static String getVerName(Context context) {   
        String verName = context.getResources()   
        .getText(R.string.app_versionName).toString();   
        return verName;   
}  


流程框架


3. 版本检查
在服务端放置最新版本的apk文件,如:http://localhost/myapp/myapp.apk
同时,在服务端放置对应此apk的版本信息调用接口或者文件,如:http://localhost/myapp/ver.json
ver.json中的内容为:

[{"appname":"jtapp12","apkname":"jtapp-12-updateapksamples.apk","verName":1.0.1,"verCode":2}]  

然后,在手机客户端上进行版本读取和检查:
  1. 01
  2. private boolean getServerVer () {  
  3. 02
  4.         try {  
  5. 03
  6.             String verjson = NetworkTool.getContent(Config.UPDATE_SERVER  
  7. 04
  8.                     + Config.UPDATE_VERJSON);  
  9. 05
  10.             JSONArray array = new JSONArray(verjson);  
  11. 06
  12.             if (array.length() > 0) {  
  13. 07
  14.                 JSONObject obj = array.getJSONObject(0);  
  15. 08
  16.                 try {  
  17. 09
  18.                     newVerCode = Integer.parseInt(obj.getString("verCode"));  
  19. 10
  20.                     newVerName = obj.getString("verName");  
  21. 11
  22.                 } catch (Exception e) {  
  23. 12
  24.                     newVerCode = -1;  
  25. 13
  26.                     newVerName = "";  
  27. 14
  28.                     return false;  
  29. 15
  30.                 }  
  31. 16
  32.             }  
  33. 17
  34.         } catch (Exception e) {  
  35. 18
  36.             Log.e(TAG, e.getMessage());  
  37. 19
  38.             return false;  
  39. 20
  40.         }  
  41. 21
  42.         return true;  
  43. 22
  44.     }
复制代码


比较服务器和客户端的版本,并进行更新操作。
  1. 1
  2. if (getServerVerCode()) {  
  3. 2
  4.          int vercode = Config.getVerCode(this); // 用到前面第一节写的方法  
  5. 3
  6.          if (newVerCode > vercode) {  
  7. 4
  8.              doNewVersionUpdate(); // 更新新版本  
  9. 5
  10.          } else {  
  11. 6
  12.              notNewVersionShow(); // 提示当前为最新版本  
  13. 7
  14.          }  
  15. 8
  16.      }
  17. 详细方法:
  18. 01
  19. private void notNewVersionShow() {  
  20. 02
  21.     int verCode = Config.getVerCode(this);  
  22. 03
  23.     String verName = Config.getVerName(this);  
  24. 04
  25.     StringBuffer sb = new StringBuffer();  
  26. 05
  27.     sb.append("当前版本:");  
  28. 06
  29.     sb.append(verName);  
  30. 07
  31.     sb.append(" Code:");  
  32. 08
  33.     sb.append(verCode);  
  34. 09
  35.     sb.append(",
  36. 10
  37. 已是最新版,无需更新!");  
  38. 11
  39.     Dialog dialog = new AlertDialog.Builder(Update.this).setTitle("软件更新")  
  40. 12
  41.             .setMessage(sb.toString())// 设置内容  
  42. 13
  43.             .setPositiveButton("确定",// 设置确定按钮  
  44. 14
  45.                     new DialogInterface.OnClickListener() {  
  46. 15
  47.                         @Override
  48. 16
  49.                         public void onClick(DialogInterface dialog,  
  50. 17
  51.                                 int which) {  
  52. 18
  53.                             finish();  
  54. 19
  55.                         }  
  56. 20
  57.                     }).create();// 创建  
  58. 21
  59.     // 显示对话框  
  60. 22
  61.     dialog.show();  
  62. 23
  63. }  
  64. 24
  65. private void doNewVersionUpdate() {  
  66. 25
  67.     int verCode = Config.getVerCode(this);  
  68. 26
  69.     String verName = Config.getVerName(this);  
  70. 27
  71.     StringBuffer sb = new StringBuffer();  
  72. 28
  73.     sb.append("当前版本:");  
  74. 29
  75.     sb.append(verName);  
  76. 30
  77.     sb.append(" Code:");  
  78. 31
  79.     sb.append(verCode);  
  80. 32
  81.     sb.append(", 发现新版本:");  
  82. 33
  83.     sb.append(newVerName);  
  84. 34
  85.     sb.append(" Code:");  
  86. 35
  87.     sb.append(newVerCode);  
  88. 36
  89.     sb.append(", 是否更新?");  
  90. 37
  91.     Dialog dialog = new AlertDialog.Builder(Update.this)  
  92. 38
  93.             .setTitle("软件更新")  
  94. 39
  95.             .setMessage(sb.toString())  
  96. 40
  97.             // 设置内容  
  98. 41
  99.             .setPositiveButton("更新",// 设置确定按钮  
  100. 42
  101.                     new DialogInterface.OnClickListener() {  
  102. 43
  103.                         @Override
  104. 44
  105.                         public void onClick(DialogInterface dialog,  
  106. 45
  107.                                 int which) {  
  108. 46
  109.                             pBar = new ProgressDialog(Update.this);  
  110. 47
  111.                             pBar.setTitle("正在下载");  
  112. 48
  113.                             pBar.setMessage("请稍候...");  
  114. 49
  115.                             pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  116. 50
  117.                             downFile(Config.UPDATE_SERVER + Config.UPDATE_APKNAME);  
  118. 51
  119.                         }  
  120. 52
  121.                     })  
  122. 53
  123.             .setNegativeButton("暂不更新",  
  124. 54
  125.                     new DialogInterface.OnClickListener() {  
  126. 55
  127.                         public void onClick(DialogInterface dialog,  
  128. 56
  129.                                 int whichButton) {  
  130. 57
  131.                             // 点击"取消"按钮之后退出程序  
  132. 58
  133.                             finish();  
  134. 59
  135.                         }  
  136. 60
  137.                     }).create();// 创建  
  138. 61
  139.     // 显示对话框  
  140. 62
  141.     dialog.show();  
  142. 63
  143. }
复制代码


4.下载模块
  1. 01
  2. void downFile(final String url) {  
  3. 02
  4.     pBar.show();  
  5. 03
  6.     new Thread() {  
  7. 04
  8.         public void run() {  
  9. 05
  10.             HttpClient client = new DefaultHttpClient();  
  11. 06
  12.             HttpGet get = new HttpGet(url);  
  13. 07
  14.             HttpResponse response;  
  15. 08
  16.             try {  
  17. 09
  18.                 response = client.execute(get);  
  19. 10
  20.                 HttpEntity entity = response.getEntity();  
  21. 11
  22.                 long length = entity.getContentLength();  
  23. 12
  24.                 InputStream is = entity.getContent();  
  25. 13
  26.                 FileOutputStream fileOutputStream = null;  
  27. 14
  28.                 if (is != null) {  
  29. 15
  30.                     File file = new File(  
  31. 16
  32.                             Environment.getExternalStorageDirectory(),  
  33. 17
  34.                             Config.UPDATE_SAVENAME);  
  35. 18
  36.                     fileOutputStream = new FileOutputStream(file);  
  37. 19
  38.                     byte[] buf = new byte[1024];  
  39. 20
  40.                     int ch = -1;  
  41. 21
  42.                     int count = 0;  
  43. 22
  44.                     while ((ch = is.read(buf)) != -1) {  
  45. 23
  46.                         fileOutputStream.write(buf, 0, ch);  
  47. 24
  48.                         count += ch;  
  49. 25
  50.                         if (length > 0) {  
  51. 26
  52.                         }  
  53. 27
  54.                     }  
  55. 28
  56.                 }  
  57. 29
  58.                 fileOutputStream.flush();  
  59. 30
  60.                 if (fileOutputStream != null) {  
  61. 31
  62.                     fileOutputStream.close();  
  63. 32
  64.                 }  
  65. 33
  66.                down();  
  67. 34
  68.             } catch (ClientProtocolException e) {  
  69. 35
  70.                 e.printStackTrace();  
  71. 36
  72.             } catch (IOException e) {  
  73. 37
  74.                 e.printStackTrace();  
  75. 38
  76.             }  
  77. 39
  78.        }  
  79. 40
  80.     }.start();  
  81. 41
  82. }
复制代码


下载完成,通过handler通知主ui线程将下载对话框取消。
  1. 1
  2. void down() {  
  3. 2
  4.         handler.post(new Runnable() {  
  5. 3
  6.            public void run() {  
  7. 4
  8.                pBar.cancel();  
  9. 5
  10.                 update();  
  11. 6
  12.             }  
  13. 7
  14.         });  
  15. 8
  16. }
  17. 5.安装应用
  18. 1
  19. void update() {  
  20. 2
  21.     Intent intent = new Intent(Intent.ACTION_VIEW);  
  22. 3
  23.     intent.setDataAndType(Uri.fromFile(new File(Environment  
  24. 4
  25.             .getExternalStorageDirectory(), Config.UPDATE_SAVENAME)),  
  26. 5
  27.             "application/vnd.android.package-archive");  
  28. 6
  29.     startActivity(intent);  
  30. 7
  31. }
复制代码









Android  应用程序自动更新源码 可用于phonegap源码下载

Android 应用程序自动更新源码.zip (200.98 KB, 下载次数: 135)




回复

使用道具 举报

0

主题

6

帖子

36

积分

新手上路

Rank: 1

积分
36
沙发
发表于 2015-1-22 19:28:43 | 只看该作者
谢谢楼主分享,正需要呢
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

ionic4视频教程

Archiver|手机版|小黑屋| PhoneGap中文网 ( 京ICP备13027796号-1 )  

GMT+8, 2024-7-27 14:34 , Processed in 0.050621 second(s), 33 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表