一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)下載器

特點(diǎn):支持多任務(wù)下載、多界面管理、可斷點(diǎn)下載


使用步驟:

1、在app下的build.gradle中加入

implementation 'com.github.Dpuntu:android-downloader:v1.0.0'

2、在Application中初始化下載器

DownloadManager.initDownloader(this);

3、配置Downloader,加入任務(wù)列表,開始下載任務(wù)

List<Downloader> downloaders= new ArrayList<Downloader>();

OkHttpClient client= new OkHttpClient.Builder().build();

Downloader downloader= new Downloader.Builder()

? ? ? ? .client(client)

? ? ? ? .fileName("android" + idtask + ".apk")??? //下載的文件存儲(chǔ)在文件夾中的名稱

? ? ? ? .filePath(Environment.getExternalStorageDirectory().getPath() + "/downFile")??? //文件存儲(chǔ)路徑

? ? ? ? .taskId("tashid")??? //該任務(wù)的唯一標(biāo)識(shí),任務(wù)id

? ? ? ? .url(idtask)????? //文件下載地址

? ? ? ? .build();

downloaders.add(downloader);

DownloadManager.addDownloader(downloaders);

DownloadManager.start(idtask);

4、下載情況監(jiān)聽

DownloadManager.subjectTask(idtask, new DownloadObserver(downlaodProgressBar, openOrDownloadBtn));

//idtask任務(wù)唯一標(biāo)識(shí)??? DownloadObserver如下,兩個(gè)參數(shù)分別是進(jìn)度條及按鈕控件,顯示進(jìn)度及當(dāng)前狀態(tài)

private class DownloadObserver implements Observer {

? ? private ProgressBar mTextView;

? ? private TextView textView;

? ? public DownloadObserver(ProgressBar textView, TextView textView1) {

? ? ? ? mTextView = textView;

? ? ? ? this.textView = textView1;

}

? ? @Override

? ? public void onCreate(String s) {

}

? ? @Override

? ? public void onReady(String s) {

}

? ? @Override

? ? public void onLoading(String s, final String s1, final long l, final long l1) {

? ? ? ? mHandler.post(new Runnable() {

? ? ? ? ? ? @Override

? ? ? ? ? ? public void run() {

? ? ? ? ? ? ? ? mTextView.setVisibility(View.VISIBLE);

? ? ? ? ? ? ? ? mTextView.setProgress((int) (((double) l1 / (double) l) * 100));

? ? ? ? ? ? ? ? textView.setText((String.format("正在下載,已完成%d%% ,下載速度: %s", (int) (((double) l1 / (double) l) * 100), s1)));

}

? ? ? ? });

}

? ? @Override

? ? public void onPause(String s, final long l, final long l1) {

? ? ? ? mHandler.post(new Runnable() {

? ? ? ? ? ? @Override

? ? ? ? ? ? public void run() {

? ? ? ? ? ? ? ? mTextView.setProgress((int) (((double) l1 / (double) l) * 100));

? ? ? ? ? ? ? ? textView.setText("繼續(xù)");

}

? ? ? ? });

}

? ? @Override

? ? public void onFinish(String s) {

? ? ? ? DownloadManager.removeTaskObserver(s, this);

? ? ? ? // 最好不要在這里做這一步操作,因?yàn)槟憧赡芙壎似渌O(jiān)聽者,有一處移除,其他的均無法監(jiān)聽甚至出現(xiàn)異常

//? DownloadManager.remove(taskId);

? ? ? ? mHandler.post(new Runnable() {

? ? ? ? ? ? @Override

? ? ? ? ? ? public void run() {

? ? ? ? ? ? ? ? mTextView.setProgress(100);

? ? ? ? ? ? ? ? mTextView.setVisibility(View.GONE);

? ? ? ? ? ? ? ? textView.setText("安裝");

? ? ? ? ? ? ? ? File file= new File(Environment.getExternalStorageDirectory().toString()

? ? ? ? ? ? ? ? ? ? ? ? + "/downFile/android" + idtask + ".apk");

? ? ? ? ? ? ? ? installAPK(getApplicationContext(), file);? //下載完成打開安裝包

}

? ? ? ? });

}

? ? @Override

? ? public void onError(String s, String s1, long l, long l1) {

? ? ? ? mHandler.post(new Runnable() {

? ? ? ? ? ? @Override

? ? ? ? ? ? public void run() {

? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(), "下載異常", Toast.LENGTH_SHORT).show();

? ? ? ? ? ? ? ? textView.setText("下載");

}

? ? ? ? });

}

}


//打開安裝包

public static void installAPK(Context context, File savedFile) {

? ? Intent intent= new Intent();

? ? intent.setAction(Intent.ACTION_VIEW);

? ? intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

? ? if (Build.VERSION.SDK_INT >= 24) { //判讀版本是否在7.0以上

//參數(shù)1 上下文, 參數(shù)2 Provider地址 和配置文件中保持一致? 參數(shù)3? 共享的文件

? ? ? ? Uri apkUri= FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", savedFile);

? ? ? ? //添加這一句表示對(duì)目標(biāo)應(yīng)用臨時(shí)授權(quán)該Uri所代表的文件

? ? ? ? intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

? ? ? ? intent.setDataAndType(apkUri, "application/vnd.android.package-archive");

? ? } else {

? ? ? ? intent.setDataAndType(Uri.fromFile(savedFile), "application/vnd.android.package-archive");

}

? ? context.startActivity(intent);

}


好了,大功告成。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容