我所开发应用不是面向大众的应用,所以无法放到应用市场去让大家下载,然后通过应用市场更新.所以我必要做一个应用自动更新功能.但是不难,Thanks to下面这篇博客:
Android应用自动更新功能的实现!!!
如果你是以前没有做过此类功能,建议你先看上面的文章.然后再来看我的.因为我也是参考了上面的实现.
其实这个自动更新功能大体就是两个三个步骤:
(1)检查更新
(2)下载更新
(3)安装更新
检查更新和下载更新其实可以算是一步.因为都比较简单,都是主要是下载.
1) 当你有新的版本发布时,在一个位置放一个更新的文件.
里面到少放有最新应用的版本号.然后你拿当前应用的版本号和服务器上的版本号对比,就知道要不要下载更新了.
2 ) 下载这个过程,对于Java来说不是什么难事,因为Java提供了丰富的API.更何况Android内置了HttpClient可用.
3) 这个,安装过程,其实就是使用一个打开查看此下载文件的 Intent.
这时需要考虑的是文件下载后放到哪里,安全否.:
一般就是先检测SD卡.然后选择一个合适的目录.
01 | private void checkUpdate() { |
03 | RequestFileInfo requestFileInfo = new RequestFileInfo(); |
04 | requestFileInfo.fileUrl = "http://www.waitab.com/demo/demo.apk" ; |
05 | String status = Environment.getExternalStorageState(); |
06 | if (!Environment.MEDIA_MOUNTED.equals(status)) { |
07 | ToastUtils.showFailure(getApplicationContext(), |
08 | "SDcard cannot use!" ); |
11 | requestFileInfo.saveFilePath = Environment |
12 | .getExternalStoragePublicDirectory( |
13 | Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(); |
14 | requestFileInfo.saveFileName = "DiTouchClient.apk" ; |
15 | showHorizontalFragmentDialog(R.string.title_wait, |
16 | R.string.title_download_update); |
17 | new DownlaodUpdateTask().execute(requestFileInfo); |
上面的进度条显示我已经封装好的了.showHorizontalFragmentDialog()
显然我使用了android-support-v4兼容包来使用Fragment的.
在进度条中有显示,下载文件大小,已经下载了多少.速度等信息.
由于涉及到网络操作.所以把这整个逻辑放在AsyncTask中.
代码如下:
001 | private class DownlaodUpdateTask extends |
002 | AsyncTask<RequestFileInfo, ProgressValue, BasicCallResult> { |
005 | protected BasicCallResult doInBackground(RequestFileInfo... params) { |
006 | final RequestFileInfo req = params[ 0 ]; |
007 | String apkFileName = "" ; |
009 | URL url = new URL(req.fileUrl); |
010 | HttpURLConnection conn = (HttpURLConnection) url |
012 | Log.i(TAG, "response code:" + conn.getResponseCode()); |
014 | if (HttpURLConnection.HTTP_OK != conn.getResponseCode()) { |
015 | return new BasicCallResult( |
016 | "Can not connect to the update Server! " , false ); |
018 | int length = conn.getContentLength(); |
019 | double total = StringUtils.bytes2M(length); |
020 | InputStream is = conn.getInputStream(); |
021 | File path = new File(req.saveFilePath); |
024 | File apkFile = new File(req.saveFilePath, req.saveFileName); |
025 | apkFileName = apkFile.getAbsolutePath(); |
026 | FileOutputStream fos = new FileOutputStream(apkFile); |
027 | ProgressValue progressValue = new ProgressValue( 0 , " downlaod…" ); |
029 | long startTime, endTime; |
030 | byte buffer[] = new byte [ 1024 ]; |
032 | startTime = System.currentTimeMillis(); |
033 | int numread = is.read(buffer); |
034 | endTime = System.currentTimeMillis(); |
040 | fos.write(buffer, 0 , numread); |
042 | double kbPerSecond = Math |
043 | .ceil((endTime - startTime) / 1000f); |
044 | double current = StringUtils.bytes2M(count); |
045 | progressValue.message = String.format( |
046 | "%.2f M/%.2f M\t\t%.2fKb/S" , total, current, |
048 | progressValue.progress = ( int ) ((( float ) count / length) * DialogUtil.LONG_PROGRESS_MAX); |
049 | publishProgress(progressValue); |
055 | } catch (MalformedURLException e) { |
057 | return new BasicCallResult( "Wrong url! " , false ); |
058 | } catch (IOException e) { |
061 | return new BasicCallResult( "Error: " + e.getLocalizedMessage(), |
064 | BasicCallResult callResult = new BasicCallResult( |
065 | "download finish!" , true ); |
066 | callResult.result = apkFileName; |
071 | protected void onPostExecute(BasicCallResult result) { |
072 | removeFragmentDialog(); |
074 | installApk(result.result); |
076 | ToastUtils.showFailure(getApplicationContext(), result.message); |
081 | protected void onProgressUpdate(ProgressValue... values) { |
082 | ProgressValue value = values[ 0 ]; |
083 | updateProgressDialog(value); |
094 | private void installApk(String fileUri) { |
095 | Intent intent = new Intent(Intent.ACTION_VIEW); |
096 | intent.setDataAndType(Uri.parse( "file://" + fileUri), |
097 | "application/vnd.android.package-archive" ); |
098 | startActivity(intent); |
PS:Java中传递或者返回多个值,我常用的办法就是将数据封装到一个对象中去.上面用到的一些封装对象如下:
传递多个值用对象是因为AsyncTask设计让你传递一个对象作为传递参数,所以传递对象也需要这样使用.
04 | * <a href="http://my.oschina.net/arthor" class="referer" target="_blank">@author</a> banxi1988 |
07 | public final class ProgressValue { |
15 | public String message; |
17 | public ProgressValue( int progress, String message) { |
19 | this .progress = progress; |
20 | this .message = message; |
基本的调用返回对象:
01 | public class BasicCallResult { |
02 | public String message; |
06 | public BasicCallResult(String message, boolean ok) { |
08 | this .message = message; |
传递下载相关信息..
1 | public class RequestFileInfo { |
3 | public String saveFilePath; |
4 | public String saveFileName; |