溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中利用DownloadManager實現(xiàn)一個文件下載功能

發(fā)布時間:2020-11-27 16:36:36 來源:億速云 閱讀:485 作者:Leah 欄目:移動開發(fā)

本篇文章為大家展示了怎么在Android中利用DownloadManager實現(xiàn)一個文件下載功能,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

Android中DownloadManager實現(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

移動網(wǎng)絡(luò) : DownloadManager.Request.NETWORK_MOBILE

Notification顯示下載進度

// 在Notification顯示下載進度
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);

下載

下載會返回一個進程ID

DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long id = downloadManager.enqueue(request);

取消下載

通過ID可以需要下載

downloadManager.remove(id);

下載完成的監(jiān)聽

下載完成,系統(tǒng)會發(fā)出廣播,通過注冊廣播監(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);

廣播接收者

注冊

<?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>

實現(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();
  }
 }
}

上述內(nèi)容就是怎么在Android中利用DownloadManager實現(xiàn)一個文件下載功能,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI