1. 准备知识
在AndroidManifest.xml里定义了每个Android apk的版本标识:
[size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 [size=1em]6 | [size=1em][size=1em]<manifest xmlns:android="http://schemas.android.com/apk/res/android" [size=1em] package="com.myapp" [size=1em] android:versionCode="1" [size=1em] android:versionName="1.0.0"> [size=1em]<application></application> [size=1em]</manifest> |
其中,android:versionCode和android:versionName两个字段分别表示版本代码,版本名称。versionCode是 整型数字,versionName是字符串。由于version是给用户看的,不太容易比较大小,升级检查时,可以以检查versionCode为主,方 便比较出版本的前后大小。
那么,在应用中如何读取AndroidManifest.xml中的versionCode和versionName呢?可以使用PackageManager的API,参考以下代码:
[size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 [size=1em]6 [size=1em]7 [size=1em]8 [size=1em]9 [size=1em]10 [size=1em]11 [size=1em]12 [size=1em]13 [size=1em]14 [size=1em]15 [size=1em]16 [size=1em]17 [size=1em]18 [size=1em]19 [size=1em]20 [size=1em]21 | [size=1em][size=1em]public static int getVerCode(Context context) { [size=1em] int verCode = -1; [size=1em] try { [size=1em] verCode = context.getPackageManager().getPackageInfo( [size=1em] "com.myapp", 0).versionCode; [size=1em] } catch (NameNotFoundException e) { [size=1em] Log.e(TAG, e.getMessage()); [size=1em] } [size=1em] return verCode; [size=1em] } [size=1em] [size=1em] public static String getVerName(Context context) { [size=1em] String verName = ""; [size=1em] try { [size=1em] verName = context.getPackageManager().getPackageInfo( [size=1em] "com.myapp", 0).versionName; [size=1em] } catch (NameNotFoundException e) { [size=1em] Log.e(TAG, e.getMessage()); [size=1em] } [size=1em] return verName; [size=1em]} |
或者在AndroidManifest中将android:versionName="1.2.0"写成 android:versionName="@string/app_versionName",然后在values/strings.xml中添加对应 字符串,这样实现之后,就可以使用如下代码获得版本名称:
[size=1em][backcolor=rgb(108, 226, 108) !important][size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 | [size=1em][size=1em]public static String getVerName(Context context) { [size=1em] String verName = context.getResources() [size=1em] .getText(R.string.app_versionName).toString(); [size=1em] return verName; [size=1em]} |
同理,apk的应用名称可以这样获得:
[size=1em][backcolor=rgb(108, 226, 108) !important][size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 | [size=1em][size=1em]public static String getAppName(Context context) { [size=1em] String verName = context.getResources() [size=1em] .getText(R.string.app_name).toString(); [size=1em] return verName; [size=1em]} |
2. 流程框架
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}]
然后,在手机客户端上进行版本读取和检查:
[size=1em][backcolor=rgb(108, 226, 108) !important][size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 [size=1em]6 [size=1em]7 [size=1em]8 [size=1em]9 [size=1em]10 [size=1em]11 [size=1em]12 [size=1em]13 [size=1em]14 [size=1em]15 [size=1em]16 [size=1em]17 [size=1em]18 [size=1em]19 [size=1em]20 [size=1em]21 [size=1em]22 | [size=1em][size=1em]private boolean getServerVer () { [size=1em] try { [size=1em] String verjson = NetworkTool.getContent(Config.UPDATE_SERVER [size=1em] + Config.UPDATE_VERJSON); [size=1em] JSONArray array = new JSONArray(verjson); [size=1em] if (array.length() > 0) { [size=1em] JSONObject obj = array.getJSONObject(0); [size=1em] try { [size=1em] newVerCode = Integer.parseInt(obj.getString("verCode")); [size=1em] newVerName = obj.getString("verName"); [size=1em] } catch (Exception e) { [size=1em] newVerCode = -1; [size=1em] newVerName = ""; [size=1em] return false; [size=1em] } [size=1em] } [size=1em] } catch (Exception e) { [size=1em] Log.e(TAG, e.getMessage()); [size=1em] return false; [size=1em] } [size=1em] return true; [size=1em] } |
比较服务器和客户端的版本,并进行更新操作。
[size=1em][backcolor=rgb(108, 226, 108) !important][size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 [size=1em]6 [size=1em]7 [size=1em]8 | [size=1em][size=1em]if (getServerVerCode()) { [size=1em] int vercode = Config.getVerCode(this); // 用到前面第一节写的方法 [size=1em] if (newVerCode > vercode) { [size=1em] doNewVersionUpdate(); // 更新新版本 [size=1em] } else { [size=1em] notNewVersionShow(); // 提示当前为最新版本 [size=1em] } [size=1em] } |
详细方法:
[size=1em][backcolor=rgb(108, 226, 108) !important][size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 [size=1em]6 [size=1em]7 [size=1em]8 [size=1em]9 [size=1em]10 [size=1em]11 [size=1em]12 [size=1em]13 [size=1em]14 [size=1em]15 [size=1em]16 [size=1em]17 [size=1em]18 [size=1em]19 [size=1em]20 [size=1em]21 [size=1em]22 [size=1em]23 [size=1em]24 [size=1em]25 [size=1em]26 [size=1em]27 [size=1em]28 [size=1em]29 [size=1em]30 [size=1em]31 [size=1em]32 [size=1em]33 [size=1em]34 [size=1em]35 [size=1em]36 [size=1em]37 [size=1em]38 [size=1em]39 [size=1em]40 [size=1em]41 [size=1em]42 [size=1em]43 [size=1em]44 [size=1em]45 [size=1em]46 [size=1em]47 [size=1em]48 [size=1em]49 [size=1em]50 [size=1em]51 [size=1em]52 [size=1em]53 [size=1em]54 [size=1em]55 [size=1em]56 [size=1em]57 [size=1em]58 [size=1em]59 [size=1em]60 [size=1em]61 [size=1em]62 | [size=1em][size=1em]private void notNewVersionShow() { [size=1em] int verCode = Config.getVerCode(this); [size=1em] String verName = Config.getVerName(this); [size=1em] StringBuffer sb = new StringBuffer(); [size=1em] sb.append("当前版本:"); [size=1em] sb.append(verName); [size=1em] sb.append(" Code:"); [size=1em] sb.append(verCode); [size=1em] sb.append(",/n已是最新版,无需更新!"); [size=1em] Dialog dialog = new AlertDialog.Builder(Update.this).setTitle("软件更新") [size=1em] .setMessage(sb.toString())// 设置内容 [size=1em] .setPositiveButton("确定",// 设置确定按钮 [size=1em] new DialogInterface.OnClickListener() { [size=1em] @Override [size=1em] public void onClick(DialogInterface dialog, [size=1em] int which) { [size=1em] finish(); [size=1em] } [size=1em] }).create();// 创建 [size=1em] // 显示对话框 [size=1em] dialog.show(); [size=1em]} [size=1em]private void doNewVersionUpdate() { [size=1em] int verCode = Config.getVerCode(this); [size=1em] String verName = Config.getVerName(this); [size=1em] StringBuffer sb = new StringBuffer(); [size=1em] sb.append("当前版本:"); [size=1em] sb.append(verName); [size=1em] sb.append(" Code:"); [size=1em] sb.append(verCode); [size=1em] sb.append(", 发现新版本:"); [size=1em] sb.append(newVerName); [size=1em] sb.append(" Code:"); [size=1em] sb.append(newVerCode); [size=1em] sb.append(", 是否更新?"); [size=1em] Dialog dialog = new AlertDialog.Builder(Update.this) [size=1em] .setTitle("软件更新") [size=1em] .setMessage(sb.toString()) [size=1em] // 设置内容 [size=1em] .setPositiveButton("更新",// 设置确定按钮 [size=1em] new DialogInterface.OnClickListener() { [size=1em] @Override [size=1em] public void onClick(DialogInterface dialog, [size=1em] int which) { [size=1em] pBar = new ProgressDialog(Update.this); [size=1em] pBar.setTitle("正在下载"); [size=1em] pBar.setMessage("请稍候..."); [size=1em] pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER); [size=1em] downFile(Config.UPDATE_SERVER + Config.UPDATE_APKNAME); [size=1em] } [size=1em] }) [size=1em] .setNegativeButton("暂不更新", [size=1em] new DialogInterface.OnClickListener() { [size=1em] public void onClick(DialogInterface dialog, [size=1em] int whichButton) { [size=1em] // 点击"取消"按钮之后退出程序 [size=1em] finish(); [size=1em] } [size=1em] }).create();// 创建 [size=1em] // 显示对话框 [size=1em] dialog.show(); [size=1em]} |
4. 下载模块
注,本部分参考了前人的相关实现,见 http://apps.hi.baidu.com/share/detail/24172508
[size=1em][backcolor=rgb(108, 226, 108) !important][size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 [size=1em]6 [size=1em]7 [size=1em]8 [size=1em]9 [size=1em]10 [size=1em]11 [size=1em]12 [size=1em]13 [size=1em]14 [size=1em]15 [size=1em]16 [size=1em]17 [size=1em]18 [size=1em]19 [size=1em]20 [size=1em]21 [size=1em]22 [size=1em]23 [size=1em]24 [size=1em]25 [size=1em]26 [size=1em]27 [size=1em]28 [size=1em]29 [size=1em]30 [size=1em]31 [size=1em]32 [size=1em]33 [size=1em]34 [size=1em]35 [size=1em]36 [size=1em]37 [size=1em]38 [size=1em]39 [size=1em]40 [size=1em]41 | [size=1em][size=1em]void downFile(final String url) { [size=1em]pBar.show(); [size=1em]new Thread() { [size=1em] public void run() { [size=1em] HttpClient client = new DefaultHttpClient(); [size=1em] HttpGet get = new HttpGet(url); [size=1em] HttpResponse response; [size=1em] try { [size=1em] response = client.execute(get); [size=1em] HttpEntity entity = response.getEntity(); [size=1em] long length = entity.getContentLength(); [size=1em] InputStream is = entity.getContent(); [size=1em] FileOutputStream fileOutputStream = null; [size=1em] if (is != null) { [size=1em] File file = new File( [size=1em] Environment.getExternalStorageDirectory(), [size=1em] Config.UPDATE_SAVENAME); [size=1em] fileOutputStream = new FileOutputStream(file); [size=1em] byte[] buf = new byte[1024]; [size=1em] int ch = -1; [size=1em] int count = 0; [size=1em] while ((ch = is.read(buf)) != -1) { [size=1em] fileOutputStream.write(buf, 0, ch); [size=1em] count += ch; [size=1em] if (length > 0) { [size=1em] } [size=1em] } [size=1em] } [size=1em] fileOutputStream.flush(); [size=1em] if (fileOutputStream != null) { [size=1em] fileOutputStream.close(); [size=1em] } [size=1em] down(); [size=1em] } catch (ClientProtocolException e) { [size=1em] e.printStackTrace(); [size=1em] } catch (IOException e) { [size=1em] e.printStackTrace(); [size=1em] } [size=1em] } [size=1em]}.start(); [size=1em]} |
下载完成,通过handler通知主ui线程将下载对话框取消。
[size=1em][backcolor=rgb(108, 226, 108) !important][size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 [size=1em]6 [size=1em]7 [size=1em]8 | [size=1em][size=1em]void down() { [size=1em] handler.post(new Runnable() { [size=1em] public void run() { [size=1em] pBar.cancel(); [size=1em] update(); [size=1em] } [size=1em] }); [size=1em]} |
5. 安装应用
[size=1em][backcolor=rgb(108, 226, 108) !important][size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 [size=1em]6 [size=1em]7 | [size=1em][size=1em]<strong> void update() { [size=1em] Intent intent = new Intent(Intent.ACTION_VIEW); [size=1em] intent.setDataAndType(Uri.fromFile(new File(Environment [size=1em] .getExternalStorageDirectory(), Config.UPDATE_SAVENAME)), [size=1em] "application/vnd.android.package-archive"); [size=1em] startActivity(intent); [size=1em] }</strong> |
如果你将apk应用发布到market上,那么,你会发现market内建了类似的模块,可以自动更新或者提醒你是否更新应用。那么,对于你自己的 应用需要自动更新的话,自己内建一个是不是更加方便了呢?本文提到的代码大多是在UpdateActivity.java中实现,为了能够使更新过程更加 友好,可以在最初launcher的Activity中建立一个线程,用来检查服务端是否有更新。有更新的时候就启动UpdateActivity,这样 的使用体验更加平滑。
欢迎光临 中科因仑“3+1”工程特种兵精英论坛 (http://bbs.enlern.com/) | Powered by Discuz! X3.4 |