Android中DownloadManager實(shí)現(xiàn)文件下載實(shí)例詳解
Android中DownloadManager實(shí)現(xiàn)文件下載
下載
創(chuàng)建下載鏈接
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
設(shè)置允許下載的網(wǎng)絡(luò)環(huán)境
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
WIFI網(wǎng)絡(luò) : DownloadManager.Request.NETWORK_WIFI
移動(dòng)網(wǎng)絡(luò) : DownloadManager.Request.NETWORK_MOBILE
Notification顯示下載進(jìn)度
// 在Notification顯示下載進(jìn)度
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
// 設(shè)置Title
request.setTitle("更新");
// 設(shè)置描述
request.setDescription("正在下載更新文件...");
設(shè)置保存路徑
private static final String DIR = "AutoUpdate"; private static final String APK = "MyHome.apk"; private static final String PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + DIR + "/" + APK; request.setDestinationInExternalPublicDir(DIR, APK);
下載
下載會(huì)返回一個(gè)進(jìn)程ID
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); long id = downloadManager.enqueue(request);
取消下載
通過(guò)ID可以需要下載
downloadManager.remove(id);
下載完成的監(jiān)聽
下載完成,系統(tǒng)會(huì)發(fā)出廣播,通過(guò)注冊(cè)廣播監(jiān)聽者可以監(jiān)聽到下載完成
廣播的Action為DownloadManager.ACTION_DOWNLOAD_COMPLETE
/** * Broadcast intent action sent by the download manager when the user clicks on a running * download, either from a system notification or from the downloads UI. */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public final static String ACTION_NOTIFICATION_CLICKED = "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED";
Code
下載
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
// WIFI狀態(tài)下下載
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
// 設(shè)置通知欄
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setTitle("更新");
request.setDescription("正在下載更新文件...");
// 存放路徑
request.setDestinationInExternalPublicDir(DIR, APK);
// 開始下載
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long id = downloadManager.enqueue(request);
廣播接收者
注冊(cè)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kongqingwei.downloadmanagerdemo">
<!-- 網(wǎng)絡(luò)權(quán)限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_USERS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".AutoUpdateBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
</receiver>
</application>
</manifest>
實(shí)現(xiàn)
package com.example.kongqingwei.downloadmanagerdemo;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* Created by kongqingwei on 2016/12/19.
* 廣播接收者
*/
public class AutoUpdateBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
Toast.makeText(context, "下載完成", Toast.LENGTH_SHORT).show();
boolean isInstalled = AutoUpdater.installApk();
Toast.makeText(context, isInstalled ? "安裝成功" : "安裝失敗", Toast.LENGTH_SHORT).show();
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android編程使用WebView實(shí)現(xiàn)文件下載功能的兩種方法
- Android文件下載進(jìn)度條的實(shí)現(xiàn)代碼
- Android zip文件下載和解壓實(shí)例
- Android實(shí)現(xiàn)文件下載進(jìn)度顯示功能
- Android 文件下載三種基本方式
- Android Retrofit文件下載進(jìn)度顯示問題的解決方法
- Android使用緩存機(jī)制實(shí)現(xiàn)文件下載及異步請(qǐng)求圖片加三級(jí)緩存
- Android 將文件下載到指定目錄的實(shí)現(xiàn)代碼
- Android文件下載功能實(shí)現(xiàn)代碼
- Android使用WebView實(shí)現(xiàn)文件下載功能
相關(guān)文章
設(shè)置Android設(shè)備WIFI在休眠時(shí)永不斷開的代碼實(shí)現(xiàn)
這篇文章主要介紹了設(shè)置Android設(shè)備WIFI在休眠時(shí)永不斷開的代碼實(shí)現(xiàn),需要的朋友可以參考下2014-07-07
Android?上實(shí)現(xiàn)DragonBones換裝功能
這篇文章主要介紹了Android?上實(shí)現(xiàn)DragonBones換裝功能,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-06-06
Android中okhttp3.4.1+retrofit2.1.0實(shí)現(xiàn)離線緩存
這篇文章主要介紹了Android中okhttp3.4.1結(jié)合retrofit2.1.0實(shí)現(xiàn)離線緩存,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android開發(fā)Flutter?桌面應(yīng)用窗口化實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了Android開發(fā)Flutter?桌面應(yīng)用窗口化實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android通過(guò)ViewModel保存數(shù)據(jù)實(shí)現(xiàn)多頁(yè)面的數(shù)據(jù)共享功能
這篇文章主要介紹了Android通過(guò)ViewModel保存數(shù)據(jù)實(shí)現(xiàn)多頁(yè)面的數(shù)據(jù)共享功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
深入理解Android中Scroller的滾動(dòng)原理
今天給大家講解的是Scroller類的滾動(dòng)實(shí)現(xiàn)原理,可能很多朋友不太了解該類是用來(lái)干嘛的,但是研究Launcher的朋友應(yīng)該對(duì)他很熟悉,Scroller類是滾動(dòng)的一個(gè)封裝類,可以實(shí)現(xiàn)View的平滑滾動(dòng)效果,而我們今天就來(lái)探究一下為什么Scroller能夠?qū)崿F(xiàn)平滑滾動(dòng)。2016-08-08
完美解決安卓jni項(xiàng)目會(huì)刪除其他so文件的問題
下面小編就為大家?guī)?lái)一篇完美解決安卓jni項(xiàng)目會(huì)刪除其他so文件的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
淺析KJFrameForAndroid框架如何高效加載Bitmap
Bitmap是Android系統(tǒng)中的圖像處理的最重要類之一。用它可以獲取圖像文件信息,進(jìn)行圖像剪切、旋轉(zhuǎn)、縮放等操作,并可以指定格式保存圖像文件。本文主要是從KJFrameForAndroid框架中分析高效加載Bitmap的方法2014-07-07
android判斷點(diǎn)擊位置是否在扇形區(qū)域內(nèi)
這篇文章主要為大家詳細(xì)介紹了android判斷點(diǎn)擊位置是否在扇形區(qū)域內(nèi),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

