xml java: ...
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:
1 import com.yolanda.nohttp.Headers; 2 import com.yolanda.nohttp.NoHttp; 3 import com.yolanda.nohttp.download.DownloadListener; 4 import com.yolanda.nohttp.download.DownloadRequest; 5 import com.yolanda.nohttp.error.ArgumentError; 6 import com.yolanda.nohttp.error.ClientError; 7 import com.yolanda.nohttp.error.NetworkError; 8 import com.yolanda.nohttp.error.ServerError; 9 import com.yolanda.nohttp.error.StorageReadWriteError; 10 import com.yolanda.nohttp.error.StorageSpaceNotEnoughError; 11 import com.yolanda.nohttp.error.TimeoutError; 12 import com.yolanda.nohttp.error.URLError; 13 import com.yolanda.nohttp.error.UnKnownHostError; 14 import com.yolanda.nohttp5.R; 15 import com.yolanda.nohttp5.config.AppConfig; 16 import com.yolanda.nohttp5.nohttp.CallServer; 17 import com.yolanda.nohttp5.util.Toast; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.os.Environment; 22 import android.view.View; 23 import android.widget.ProgressBar; 24 import android.widget.TextView; 25 26 public class DownloadActivity extends Activity implements View.OnClickListener, DownloadListener { 27 28 private final static String PROGRESS_KEY = "download_progress"; 29 /** 30 * 下載按鈕、暫停、開始等. 31 */ 32 private TextView mBtnStart; 33 /** 34 * 下載狀態. 35 */ 36 private TextView mTvResult; 37 /** 38 * 下載進度條. 39 */ 40 private ProgressBar mProgressBar; 41 /*** 42 * 下載地址. 43 */ 44 private String url = "http://m.apk.67mo.com/apk/999129_21769077_1443483983292.apk"; 45 /** 46 * 下載請求. 47 */ 48 private DownloadRequest downloadRequest; 49 50 @Override 51 protected void onCreate(Bundle savedInstanceState) { 52 super.onCreate(savedInstanceState); 53 setContentView(R.layout.activity_download); 54 55 mProgressBar = (ProgressBar) findViewById(R.id.pb_progress); 56 mBtnStart = (TextView) findViewById(R.id.btn_start_download); 57 mTvResult = (TextView) findViewById(R.id.tv_result); 58 mBtnStart.setOnClickListener(this); 59 60 // url 下載地址 61 // fileFolder 保存的文件夾 62 // fileName 文件名 63 // isRange 是否斷點續傳下載 64 // isDeleteOld 如果發現文件已經存在是否刪除後重新下載 65 String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 66 downloadRequest = NoHttp.createDownloadRequest(url, path, "nohttp.apk", true, false); 67 68 // 檢查已經下載了一半的文件是什麼狀態 69 int status = downloadRequest.checkBeforeStatus(); 70 if (status == DownloadRequest.STATUS_FINISH) {// 文件已經下載完成 71 mTvResult.setText("下載完成"); 72 mBtnStart.setText("已經下載完成"); 73 // ... 提示用戶安裝apk 74 } else if (status == DownloadRequest.STATUS_RESTART) {// 代表文件不存在,需要從頭下載 75 mTvResult.setText(""); 76 mBtnStart.setText("下載"); 77 } else if (status == DownloadRequest.STATUS_RESUME) {// 代表文件已經下載了一半了 78 int progress = AppConfig.getInstance().getInt(PROGRESS_KEY, 0); 79 mProgressBar.setProgress(progress); 80 mTvResult.setText("已經下載了 " + progress + "%"); 81 mBtnStart.setText("繼續下載"); 82 } 83 } 84 85 @Override 86 public void onClick(View v) { 87 if (downloadRequest.isStarted()) { 88 // 暫停下載 89 downloadRequest.cancel(true); 90 } else { 91 // what 區分下載 92 // downloadRequest 下載請求對象 93 // downloadListener 下載監聽 94 CallServer.getDownloadInstance().add(0, downloadRequest, this); 95 } 96 } 97 98 /** 99 * @param what 代表哪一個下載 100 * @param isResume 是不是斷點續續傳開始下載的 101 * @param beforeLenght 斷點開始的地方的文件大小 102 * @param headers 本地請求的時候的響應頭 103 * @param allCount 本次需要下載多少 104 */ 105 @Override 106 public void onStart(int what, boolean isResume, long beforeLenght, Headers headers, long allCount) { 107 int progress = AppConfig.getInstance().getInt(PROGRESS_KEY, 0); 108 mTvResult.setText("已下載: " + progress + "%"); 109 mBtnStart.setText("暫停"); 110 } 111 112 @Override 113 public void onProgress(int what, int progress, long fileCount) { 114 AppConfig.getInstance().putInt(PROGRESS_KEY, progress); 115 mProgressBar.setProgress(progress); 116 mTvResult.setText("已經下載了 " + progress + "%"); 117 } 118 119 @Override 120 public void onDownloadError(int what, Exception exception) { 121 mBtnStart.setText("再次嘗試"); 122 123 String message = "下載出錯了:"; 124 if (exception instanceof ClientError) { 125 message += "客戶端錯誤"; 126 } else if (exception instanceof ServerError) { 127 message += "伺服器發生內部錯誤"; 128 } else if (exception instanceof NetworkError) { 129 message += "網路不可用,請檢查網路"; 130 } else if (exception instanceof StorageReadWriteError) { 131 message += "存儲卡錯誤,請檢查存儲卡"; 132 } else if (exception instanceof StorageSpaceNotEnoughError) { 133 message += "存儲位置空間不足"; 134 } else if (exception instanceof TimeoutError) { 135 message += "下載超時"; 136 } else if (exception instanceof UnKnownHostError) { 137 message += "伺服器找不到"; 138 } else if (exception instanceof URLError) { 139 message += "url地址錯誤"; 140 } else if (exception instanceof ArgumentError) { 141 message += "下載參數錯誤"; 142 } else { 143 message += "未知錯誤"; 144 } 145 mTvResult.setText(message); 146 } 147 148 @Override 149 public void onFinish(int what, String filePath) { 150 Toast.show("下載完成"); 151 mTvResult.setText("下載完成,文件保存在:" + filePath); 152 } 153 154 @Override 155 public void onCancel(int what) { 156 mTvResult.setText("下載暫停"); 157 mBtnStart.setText("繼續下載"); 158 } 159 160 }