我们开发的app一开始肯定都是1.0版本的,但应用上线后公司肯定后期会对应用进行维护对一些Bug修复。这时候新的版本出来了我们就可以通过自己的应用来检查是否有新版本,如果有新版本就可以让用户直接下载安装就不用再去应用市场搜索下载了。
1.先来说一下实现思路:每次启动应用我们就获取放在服务器上的更新日志(最好保存了最新的版本号,更新内容说明,apk下载地址),我们获取到版本号与当前应用的版本好进行对比,这样我们就可以知道应用是否更新了,废话不多说直接上代码。。
2.先来说说versionCode和versionName
versionCode 1 versionName "1.0"
2.获取服务器上的更新日志信息,需要放在线程中执行哦!
private final String path = "http://www.zgylzb.com/.../update.asp"; /** * 获取升级信息 * @return * @throws Exception */ public UpdateInfo getUpDateInfo() throws Exception { StringBuilder sb = new StringBuilder(); String line; BufferedReader reader = null; UpdateInfo updateInfo; try { URL url = new URL(path); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setConnectTimeout(3000); urlConnection.setReadTimeout(3000); if (urlConnection.getResponseCode() == 200) { reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8")); while ((line = reader.readLine()) != null) { sb.append(line); } String info = sb.toString(); updateInfo = new UpdateInfo(); updateInfo.setVersion(info.split("&")[1]); updateInfo.setDescription(info.split("&")[2]); updateInfo.setUrl(info.split("&")[3]); return updateInfo; } } catch (UnknownHostException e) { return null; } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (Exception e) { e.printStackTrace(); } } return null; }
- 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
- 53
- 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
- 53
服务器返回的更新数据,最好就使用JSON返回。
3.这里进行网络访问所以自行添加Internet权限,同时还需要对手机网络进行判断等一系列处理这里就不贴出来了。获取到了版本信息就需要与当前版本进行对比了
if (isNeedUpdate()) { showUpdateDialog(); }
/** * 判断apk是否需要升级 * * @return true需要| false不需要 */ private boolean isNeedUpdate() { int v = Integer.parseInt(info.getVersion()); if (server > getVersion())) { return true; } else { return false; } }
private int getVersion() { try { PackageManager packageManager = getPackageManager(); PackageInfo packageInfo = packageManager.getPackageInfo( getPackageName(), 0); return packageInfo.versionCode; } catch (Exception e) { e.printStackTrace(); return -1; } }
4.需要进行升级,那就把升级信息用对话框展示给用户
/** * 显示升级信息的对话框 */ private void showUpdateDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(WelcomeActivity.this); builder.setIcon(android.R.drawable.ic_dialog_info); builder.setTitle("请升级APP版本至" + info.getVersion()); builder.setMessage(info.getDescription()); builder.setCancelable(false); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { downFile(info.getUrl()); } else { Toast.makeText(WelcomeActivity.this, "SD卡不可用,请插入SD卡", Toast.LENGTH_SHORT).show(); } } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create().show(); }
- 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
- 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
5.下载最新版本的apk
/** * 下载最新版本的apk * * @param path apk下载地址 */ private void downFile(final String path) { pBar = new ProgressDialog(WelcomeActivity.this); pBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pBar.setCancelable(false); pBar.setTitle("正在下载..."); pBar.setMessage("请稍候..."); pBar.setProgress(0); pBar.show(); new Thread() { public void run() { try { URL url = new URL(path); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setReadTimeout(5000); con.setConnectTimeout(5000); con.setRequestProperty("Charset", "UTF-8"); con.setRequestMethod("GET"); if (con.getResponseCode() == 200) { int length = con.getContentLength(); InputStream is = con.getInputStream(); pBar.setMax(length); FileOutputStream fileOutputStream = null; if (is != null) { File file = new File(Environment.getExternalStorageDirectory() .getAbsolutePath(), "home.apk"); fileOutputStream = new FileOutputStream(file); byte[] buf = new byte[1024]; int ch; int process = 0; while ((ch = is.read(buf)) != -1) { fileOutputStream.write(buf, 0, ch); process += ch; pBar.setProgress(process); } } if (fileOutputStream != null) { fileOutputStream.flush(); fileOutputStream.close(); } handler.sendEmptyMessage(0); } } catch (Exception e) { e.printStackTrace(); } } }.start(); }
- 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
- 53
- 54
- 55
- 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
- 53
- 54
- 55
6.对apk进行安装
//将下载进度对话框取消pBar.cancel()//安装apk,也可以进行静默安装Intent intent = new Intent(Intent.ACTION_VIEW)intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "home.apk")), "application/vnd.android.package-archive")startActivityForResult(intent, 10)
版本更新基本是就这几个步骤,具体实现还是需要根据公司需求。
转载:http://blog.csdn.net/a_zhon/article/details/52744725
1 0