Android项目 手机安全卫士(代码最全,注释最详细)之六 apk下载

------- 源自梦想永远是你IT事业的好友、只是勇敢地说出我学到! ----------

按惯例,写在前面的:可能在学习Android的过程中,大家会和我一样,学习过大量的基础知识,很多的知识点也都能说出了123来,但是这些孤立的点终究显得太零散了,因此,我想从今天开始,以最经典的手机安全卫士项目为例,自己锻炼一下,也是想和大家交流交流,希望大家都能给出见解,共同进步。

六、下载apk

(afinal框架实现,多线程断点下载) afinal简介:http://blog.csdn.net/dongdong230/article/details/11751003
把afinal的jar包放到工程的libs目录下。
apk文件在工程的bin目录下,在清单文件中更改文件版本,再将更改后的apk文件放进tomcat环境下的工程中,这样就得到了升级后的版本文件。

具体代码:

			FinalHttp finalHttp = new FinalHttp();				//创建FinalHttp对象,用于多线程断点下载
			File file = new File(Environment.getExternalStorageDirectory(),"temp.apk");		//设置存放目录及存放文件的名称
			finalHttp.download(updateInfo.getApkurl(), file.getAbsolutePath(), new AjaxCallBack<File>(){	//下载,复写3方法


				/**
				 * 若下载失败,打印出错误
				 */
				@Override
				public void onFailure(Throwable t, int errorNo,
						String strMsg) {
					t.printStackTrace();
					super.onFailure(t, errorNo, strMsg);
				}
				
				/**
				 * 文件下载过程中调用的方法(进度)
				 * @param count	文件总长度
				 * @param current	当前下载的进度	
				 */
				@Override
				public void onLoading(long count, long current) {
					int progress = (int)(current*100/count);
					tv_splash_progress.setText("下载进度:"+progress+"%");
					super.onLoading(count, current);
				}
				/**
				 * 文件下载成功调用的方法
				 */
				@Override
				public void onSuccess(File t) {
					Toast.makeText(getApplicationContext(), "下载成功,请进行替换安装", Toast.LENGTH_SHORT).show();
					super.onSuccess(t);
				}
			});




在布局文件中加一个TextView用于显示下载进度,开始设为隐藏:
具体代码:

			    <TextView
					 android:id="@+id/tv_splash_progress"
					 android:layout_width="wrap_content"
					 android:layout_height="wrap_content"
					 android:textColor="#ff0000"
					 android:visibility="invisible"
					 android:layout_below="@+id/progressBar1"
					 android:layout_centerHorizontal="true"
					 android:layout_marginTop="16dp"/>



下载之前将其设为可见,即

				tv_splash_progress.setVisibility(View.VISIBLE);

注意设置写sdcard的权限:WRITE_EXTERNAL_STORAGE


------- 源自梦想永远是你IT事业的好友、只是勇敢地说出我学到! ----------


原文地址:https://www.cnblogs.com/pangblog/p/3327352.html