转的:适合新手学习,但在实际项目中不可这么做。 以下是我转的内容: =================================================================== android download dialog file string buffer

各种平台软件更新做法差不多,大体做法如下:

1.服务器存放一个最新版本的xml文件,或者存在数据库,各种做法都可以。

1.1xml文件存储verCode verName updateUrl等信息

1.2用户安装的软件的verCode和服务器上取得的做比较,检测到版本低就更新

2.下面是Android手机软件版本更新的教程

2.1项目目录

2.2编写UpdateManager类

[java] view plain copy print ?
  1. packagecom.doublejun.update;
  2. importjava.io.File;
  3. importjava.io.FileOutputStream;
  4. importjava.io.IOException;
  5. importjava.io.InputStream;
  6. importjava.net.HttpURLConnection;
  7. importjava.net.MalformedURLException;
  8. importjava.net.URL;
  9. importandroid.app.AlertDialog;
  10. importandroid.app.AlertDialog.Builder;
  11. importandroid.app.Dialog;
  12. importandroid.content.Context;
  13. importandroid.content.DialogInterface;
  14. importandroid.content.Intent;
  15. importandroid.content.DialogInterface.OnClickListener;
  16. importandroid.content.IntentFilter;
  17. importandroid.net.Uri;
  18. importandroid.os.Handler;
  19. importandroid.os.Message;
  20. importandroid.util.Log;
  21. importandroid.view.LayoutInflater;
  22. importandroid.view.View;
  23. importandroid.widget.ProgressBar;
  24. publicclassUpdateManager{
  25. privateContextmContext;
  26. privatefinalStringupdateMsg="亲,有新版本,快下载吧!";//下载消息提示
  27. privateDialognoticeDialog;//下载提示对话框
  28. privateDialogdownloadDialog;//下载进度对话框
  29. privateProgressBarmProgressBar;//进度条
  30. privateBooleaninterceptFlag=false;//标记用户是否在下载过程中取消下载
  31. privateThreaddownloadApkThread=null;//下载线程
  32. privatefinalStringapkUrl="http://www.ypdm.com/hyjj.apk";//apk的URL地址
  33. privatefinalStringsavePath="/sdcard/UpdateDemo/";//下载的apk存放的路径
  34. privatefinalStringsaveFileName=savePath+"hyjjrelease.apk";//下载的apk文件
  35. privateintprogress=0;//下载进度
  36. privatefinalintDOWNLOAD_ING=1;//标记正在下载
  37. privatefinalintDOWNLOAD_OVER=2;//标记下载完成
  38. privatefinalStringTAG="版本更新";//日志打印标签
  39. privateHandlermhandler=newHandler(){//更新UI的handler
  40. @Override
  41. publicvoidhandleMessage(Messagemsg){
  42. super.handleMessage(msg);
  43. switch(msg.what){
  44. caseDOWNLOAD_ING:
  45. //更新进度条
  46. mProgressBar.setProgress(progress);
  47. break;
  48. caseDOWNLOAD_OVER:
  49. downloadDialog.dismiss();
  50. installApk();
  51. //安装
  52. break;
  53. default:
  54. break;
  55. }
  56. }
  57. };
  58. /*
  59. *构造方法
  60. */
  61. publicUpdateManager(Contextcontext){
  62. this.mContext=context;
  63. }
  64. /*
  65. *检查是否有需要更新,具体比较版本xml
  66. */
  67. publicvoidcheckUpdate(){
  68. //到服务器检查软件是否有新版本
  69. //如果有则
  70. showNoticeDialog();
  71. }
  72. /*
  73. *显示版本更新对话框
  74. */
  75. privatevoidshowNoticeDialog(){
  76. AlertDialog.Builderbuilder=newBuilder(mContext);
  77. builder.setTitle("版本更新");
  78. builder.setMessage(updateMsg);
  79. builder.setPositiveButton("更新",newOnClickListener(){
  80. publicvoidonClick(DialogInterfacedialog,intwhich){
  81. noticeDialog.dismiss();
  82. showDownloadDialog();
  83. }
  84. });
  85. builder.setNegativeButton("以后再说",newOnClickListener(){
  86. publicvoidonClick(DialogInterfacedialog,intwhich){
  87. noticeDialog.dismiss();
  88. }
  89. });
  90. noticeDialog=builder.create();
  91. noticeDialog.show();
  92. }
  93. /*
  94. *弹出下载进度对话框
  95. */
  96. privatevoidshowDownloadDialog(){
  97. AlertDialog.Builderbuilder=newBuilder(mContext);
  98. builder.setTitle("软件更新");
  99. finalLayoutInflaterinflater=LayoutInflater.from(mContext);
  100. Viewv=inflater.inflate(R.layout.progress,null);
  101. mProgressBar=(ProgressBar)v.findViewById(R.id.updateProgress);
  102. builder.setView(v);
  103. builder.setNegativeButton("取消",newOnClickListener(){
  104. publicvoidonClick(DialogInterfacedialog,intwhich){
  105. downloadDialog.dismiss();
  106. interceptFlag=true;
  107. }
  108. });
  109. downloadDialog=builder.create();
  110. downloadDialog.show();
  111. downloadLatestVersionApk();
  112. }
  113. /*
  114. *下载最新的apk文件
  115. */
  116. privatevoiddownloadLatestVersionApk(){
  117. downloadApkThread=newThread(downloadApkRunnable);
  118. downloadApkThread.start();
  119. }
  120. //匿名内部类,apk文件下载线程
  121. privateRunnabledownloadApkRunnable=newRunnable(){
  122. publicvoidrun(){
  123. try{
  124. URLurl=newURL(apkUrl);
  125. HttpURLConnectionconn=(HttpURLConnection)url
  126. .openConnection();
  127. conn.connect();
  128. intlength=conn.getContentLength();
  129. Log.e(TAG,"总字节数:"+length);
  130. InputStreamis=conn.getInputStream();
  131. Filefile=newFile(savePath);
  132. if(!file.exists()){
  133. file.mkdir();
  134. }
  135. FileapkFile=newFile(saveFileName);
  136. FileOutputStreamout=newFileOutputStream(apkFile);
  137. intcount=0;
  138. intreadnum=0;
  139. byte[]buffer=newbyte[1024];
  140. do{
  141. readnum=is.read(buffer);
  142. count+=readnum;
  143. progress=(int)(((float)count/length)*100);
  144. Log.e(TAG,"下载进度"+progress);
  145. mhandler.sendEmptyMessage(DOWNLOAD_ING);
  146. if(readnum<=0){
  147. //下载结束
  148. mhandler.sendEmptyMessage(DOWNLOAD_OVER);
  149. break;
  150. }
  151. out.write(buffer,0,readnum);
  152. }while(!interceptFlag);
  153. is.close();
  154. out.close();
  155. }catch(MalformedURLExceptione){
  156. e.printStackTrace();
  157. }catch(IOExceptione){
  158. e.printStackTrace();
  159. }
  160. }
  161. };
  162. /*
  163. *安装下载的apk文件
  164. */
  165. privatevoidinstallApk(){
  166. Filefile=newFile(saveFileName);
  167. if(!file.exists()){
  168. return;
  169. }
  170. Intentintent=newIntent(Intent.ACTION_VIEW);
  171. intent.setDataAndType(Uri.parse("file://"+file.toString()),"application/vnd.android.package-archive");
  172. mContext.startActivity(intent);
  173. }
  174. }

以上代码注释足够详细。不解释了。。

2.3在Main中调用试试吧。

[java] view plain copy print ?
  1. packagecom.doublejun.update;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. publicclassMainextendsActivity{
  5. /**Calledwhentheactivityisfirstcreated.*/
  6. @Override
  7. publicvoidonCreate(BundlesavedInstanceState){
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. UpdateManagerupdate=newUpdateManager(this);
  11. update.checkUpdate();
  12. }
  13. }

好吧,就这些。希望对你有帮助。

更多相关文章

  1. 解决Android(安卓)SDK Manager更新、下载速度慢
  2. Android在layout xml中使用include .
  3. Android(安卓)RecyclerView更新某条/一条数据
  4. Android(安卓)productFalvors 多渠道打包方案
  5. Android使用SimpleAdapter更新ListView里面的Drawable元素
  6. android上如何写配置文件
  7. 菜单之二:使用xml文件定义菜单
  8. Android中sqllite存储海量数据解决办法
  9. Android点击事件的实现

随机推荐

  1. Java(Android)线程池
  2. Android(安卓)开发调试工具的使用总结
  3. 使用android studio 来开发hello world a
  4. Android开发常用属性
  5. 关于android中几个Info系列类的总结
  6. Android之SQLite开发(2)—SQLiteOpenHelper
  7. 一个不错的启动菜单显示屏动画效果
  8. android的tabHost的使用
  9. Android---App Inventor环境搭建
  10. Android(安卓): gen already exists but