首页 技术 正文
技术 2022年11月8日
0 收藏 639 点赞 1,425 浏览 4759 个字

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <ProgressBar
android:id="@+id/pb_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" /> <TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:id="@+id/btn_start_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="下载" /></LinearLayout>

java:

 import com.yolanda.nohttp.Headers;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.download.DownloadListener;
import com.yolanda.nohttp.download.DownloadRequest;
import com.yolanda.nohttp.error.ArgumentError;
import com.yolanda.nohttp.error.ClientError;
import com.yolanda.nohttp.error.NetworkError;
import com.yolanda.nohttp.error.ServerError;
import com.yolanda.nohttp.error.StorageReadWriteError;
import com.yolanda.nohttp.error.StorageSpaceNotEnoughError;
import com.yolanda.nohttp.error.TimeoutError;
import com.yolanda.nohttp.error.URLError;
import com.yolanda.nohttp.error.UnKnownHostError;
import com.yolanda.nohttp5.R;
import com.yolanda.nohttp5.config.AppConfig;
import com.yolanda.nohttp5.nohttp.CallServer;
import com.yolanda.nohttp5.util.Toast; import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView; public class DownloadActivity extends Activity implements View.OnClickListener, DownloadListener { private final static String PROGRESS_KEY = "download_progress";
/**
* 下载按钮、暂停、开始等.
*/
private TextView mBtnStart;
/**
* 下载状态.
*/
private TextView mTvResult;
/**
* 下载进度条.
*/
private ProgressBar mProgressBar;
/***
* 下载地址.
*/
private String url = "http://m.apk.67mo.com/apk/999129_21769077_1443483983292.apk";
/**
* 下载请求.
*/
private DownloadRequest downloadRequest; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download); mProgressBar = (ProgressBar) findViewById(R.id.pb_progress);
mBtnStart = (TextView) findViewById(R.id.btn_start_download);
mTvResult = (TextView) findViewById(R.id.tv_result);
mBtnStart.setOnClickListener(this); // url 下载地址
// fileFolder 保存的文件夹
// fileName 文件名
// isRange 是否断点续传下载
// isDeleteOld 如果发现文件已经存在是否删除后重新下载
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
downloadRequest = NoHttp.createDownloadRequest(url, path, "nohttp.apk", true, false); // 检查已经下载了一半的文件是什么状态
int status = downloadRequest.checkBeforeStatus();
if (status == DownloadRequest.STATUS_FINISH) {// 文件已经下载完成
mTvResult.setText("下载完成");
mBtnStart.setText("已经下载完成");
// ... 提示用户安装apk
} else if (status == DownloadRequest.STATUS_RESTART) {// 代表文件不存在,需要从头下载
mTvResult.setText("");
mBtnStart.setText("下载");
} else if (status == DownloadRequest.STATUS_RESUME) {// 代表文件已经下载了一半了
int progress = AppConfig.getInstance().getInt(PROGRESS_KEY, );
mProgressBar.setProgress(progress);
mTvResult.setText("已经下载了 " + progress + "%");
mBtnStart.setText("继续下载");
}
} @Override
public void onClick(View v) {
if (downloadRequest.isStarted()) {
// 暂停下载
downloadRequest.cancel(true);
} else {
// what 区分下载
// downloadRequest 下载请求对象
// downloadListener 下载监听
CallServer.getDownloadInstance().add(, downloadRequest, this);
}
} /**
* @param what 代表哪一个下载
* @param isResume 是不是断点续续传开始下载的
* @param beforeLenght 断点开始的地方的文件大小
* @param headers 本地请求的时候的响应头
* @param allCount 本次需要下载多少
*/
@Override
public void onStart(int what, boolean isResume, long beforeLenght, Headers headers, long allCount) {
int progress = AppConfig.getInstance().getInt(PROGRESS_KEY, );
mTvResult.setText("已下载: " + progress + "%");
mBtnStart.setText("暂停");
} @Override
public void onProgress(int what, int progress, long fileCount) {
AppConfig.getInstance().putInt(PROGRESS_KEY, progress);
mProgressBar.setProgress(progress);
mTvResult.setText("已经下载了 " + progress + "%");
} @Override
public void onDownloadError(int what, Exception exception) {
mBtnStart.setText("再次尝试"); String message = "下载出错了:";
if (exception instanceof ClientError) {
message += "客户端错误";
} else if (exception instanceof ServerError) {
message += "服务器发生内部错误";
} else if (exception instanceof NetworkError) {
message += "网络不可用,请检查网络";
} else if (exception instanceof StorageReadWriteError) {
message += "存储卡错误,请检查存储卡";
} else if (exception instanceof StorageSpaceNotEnoughError) {
message += "存储位置空间不足";
} else if (exception instanceof TimeoutError) {
message += "下载超时";
} else if (exception instanceof UnKnownHostError) {
message += "服务器找不到";
} else if (exception instanceof URLError) {
message += "url地址错误";
} else if (exception instanceof ArgumentError) {
message += "下载参数错误";
} else {
message += "未知错误";
}
mTvResult.setText(message);
} @Override
public void onFinish(int what, String filePath) {
Toast.show("下载完成");
mTvResult.setText("下载完成,文件保存在:" + filePath);
} @Override
public void onCancel(int what) {
mTvResult.setText("下载暂停");
mBtnStart.setText("继续下载");
} }

NoHttp封装–05 文件下载

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291