实例介绍
【实例简介】
关于客户端
IP更改:在Common.java页面,SERVER_IP变量标识服务器地址,运行前需要改成自己服务器IP;
关于服务器端
1、服务器端工程写于zendstudio中,可利用其打开
2、将try_downloadFile_progress_server文件夹放在apache服务器的htdocs文件夹下。
【实例截图】
【核心代码】
客户端代码:
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | package com.example.try_downloadfile_progress; /** * @author harvic * @date 2014-5-7 * @address http://blog.csdn.net/harvic880925 */ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.json.JSONArray; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button m_btnCheckNewestVersion; long m_newVerCode; //最新版的版本号 String m_newVerName; //最新版的版本名 String m_appNameStr; //下载到本地要给这个APP命的名字 Handler m_mainHandler; ProgressDialog m_progressDlg; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化相关变量 initVariable(); m_btnCheckNewestVersion.setOnClickListener(btnClickListener); } private void initVariable() { m_btnCheckNewestVersion = (Button)findViewById(R.id.chek_newest_version); m_mainHandler = new Handler(); m_progressDlg = new ProgressDialog( this ); m_progressDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确 m_progressDlg.setIndeterminate( false ); m_appNameStr = "haha.apk" ; } OnClickListener btnClickListener = new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new checkNewestVersionAsyncTask().execute(); } }; class checkNewestVersionAsyncTask extends AsyncTask<Void, Void, Boolean> { @Override protected Boolean doInBackground(Void... params) { // TODO Auto-generated method stub if (postCheckNewestVersionCommand2Server()) { int vercode = Common.getVerCode(getApplicationContext()); // 用到前面第一节写的方法 if (m_newVerCode > vercode) { return true ; } else { return false ; } } return false ; } @Override protected void onPostExecute(Boolean result) { // TODO Auto-generated method stub if (result) { //如果有最新版本 doNewVersionUpdate(); // 更新新版本 } else { notNewVersionDlgShow(); // 提示当前为最新版本 } super .onPostExecute(result); } @Override protected void onPreExecute() { // TODO Auto-generated method stub super .onPreExecute(); } } /** * 从服务器获取当前最新版本号,如果成功返回TURE,如果失败,返回FALSE * @return */ private Boolean postCheckNewestVersionCommand2Server() { StringBuilder builder = new StringBuilder(); JSONArray jsonArray = null ; try { // 构造POST方法的{name:value} 参数对 List<NameValuePair> vps = new ArrayList<NameValuePair>(); // 将参数传入post方法中 vps.add( new BasicNameValuePair( "action" , "checkNewestVersion" )); builder = Common.post_to_server(vps); Log.e( "msg" , builder.toString()); jsonArray = new JSONArray(builder.toString()); if (jsonArray.length()> 0 ) { if (jsonArray.getJSONObject( 0 ).getInt( "id" ) == 1 ) { m_newVerName = jsonArray.getJSONObject( 0 ).getString( "verName" ); m_newVerCode = jsonArray.getJSONObject( 0 ).getLong( "verCode" ); return true ; } } return false ; } catch (Exception e) { Log.e( "msg" ,e.getMessage()); m_newVerName= "" ; m_newVerCode=- 1 ; return false ; } } /** * 提示更新新版本 */ private void doNewVersionUpdate() { int verCode = Common.getVerCode(getApplicationContext()); String verName = Common.getVerName(getApplicationContext()); String str= "当前版本:" verName " Code:" verCode " ,发现新版本:" m_newVerName " Code:" m_newVerCode " ,是否更新?" ; Dialog dialog = new AlertDialog.Builder( this ).setTitle( "软件更新" ).setMessage(str) // 设置内容 .setPositiveButton( "更新" , // 设置确定按钮 new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { m_progressDlg.setTitle( "正在下载" ); m_progressDlg.setMessage( "请稍候..." ); downFile(Common.UPDATESOFTADDRESS); //开始下载 } }) .setNegativeButton( "暂不更新" , new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // 点击"取消"按钮之后退出程序 finish(); } }).create(); // 创建 // 显示对话框 dialog.show(); } /** * 提示当前为最新版本 */ private void notNewVersionDlgShow() { int verCode = Common.getVerCode( this ); String verName = Common.getVerName( this ); String str= "当前版本:" verName " Code:" verCode ",/n已是最新版,无需更新!" ; Dialog dialog = new AlertDialog.Builder( this ).setTitle( "软件更新" ) .setMessage(str) // 设置内容 .setPositiveButton( "确定" , // 设置确定按钮 new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }).create(); // 创建 // 显示对话框 dialog.show(); } private void downFile( final String url) { m_progressDlg.show(); new Thread() { public void run() { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse response; try { response = client.execute(get); HttpEntity entity = response.getEntity(); long length = entity.getContentLength(); m_progressDlg.setMax(( int )length); //设置进度条的最大值 InputStream is = entity.getContent(); FileOutputStream fileOutputStream = null ; if (is != null ) { File file = new File( Environment.getExternalStorageDirectory(), m_appNameStr); fileOutputStream = new FileOutputStream(file); byte [] buf = new byte [ 1024 ]; int ch = - 1 ; int count = 0 ; while ((ch = is.read(buf)) != - 1 ) { fileOutputStream.write(buf, 0 , ch); count = ch; if (length > 0 ) { m_progressDlg.setProgress(count); } } } fileOutputStream.flush(); if (fileOutputStream != null ) { fileOutputStream.close(); } down(); //告诉HANDER已经下载完成了,可以安装了 } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } /** * 告诉HANDER已经下载完成了,可以安装了 */ private void down() { m_mainHandler.post( new Runnable() { public void run() { m_progressDlg.cancel(); update(); } }); } /** * 安装程序 */ void update() { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile( new File(Environment .getExternalStorageDirectory(), m_appNameStr)), "application/vnd.android.package-archive" ); startActivity(intent); } } |
服务器端代码:
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 | <?php header ( 'Content-type:text/json' ); // 这句是重点,它告诉接收数据的对象此页面输出的是json数据; /** *整个程序的返回值是这样定义的:首先返回的JSON类型数据 *程序运行正确,id=1;运行出错,id=0; *value字段,对应状态码 */ $action = $_POST [ 'action' ]; switch ( $action ){ case 'checkNewestVersion' : //检查最新版本 checkNewestVersion(); break ; default : break ; } /** * 查询当前最新版本号 */ function checkNewestVersion() { $returnArray = array (); $a = array ( 'id' => "1" , 'verName' => "1.0.1" , 'verCode' => "2" ); $returnArray [] = $a ; $returnValue = json_encode ( $returnArray ); echo $returnValue ; } |
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论