1. 可选
  2. 代码

主要使用DownloadManager.Request进行下载。

完整的项目:https://github.com/lijiancheng0614/android-NetworkConnect

参考:http://developer.android.com/reference/android/app/DownloadManager.html

可选

  • 注册一个BroadcastReceiver来监听下载完成,并使用Callback来执行下载完成后的事件。

    1
    2
    IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    context.registerReceiver(downloadReceiver, filter);

  • 使用DownloadManager.Query查询是否下载成功。

  • 设置Notification。

    1
    request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

  • 结合网络状况下载。

    1
    2
    3
    4
    // Restrict the types of networks over which this download may proceed.
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    // Set whether this download may proceed over a roaming connection.
    request.setAllowedOverRoaming(false);

代码

  • DownloadHelper.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    package com.example.android.networkconnect;

    import android.app.DownloadManager;
    import android.app.DownloadManager.Request;
    import android.app.DownloadManager.Query;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Environment;

    /**
    * Download file helper.
    */
    public class DownloadHelper {
    private DownloadManager downloadManager;
    private Request request;
    private long downloadId = -1;
    /**
    * Broadcast when the download has successfully completed.
    */
    private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
    long completeDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
    if (completeDownloadId == downloadId) {
    // if download successful
    if (getInt(downloadId, DownloadManager.COLUMN_STATUS) == DownloadManager.STATUS_SUCCESSFUL) {
    mDownloadDoneCallback.onDownloadDone();
    }
    }
    }
    };
    private DownloadDoneCallback mDownloadDoneCallback;

    public DownloadHelper(Context context, String uri, DownloadDoneCallback downloadDoneCallback) throws Exception {
    downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Uri downloadUri = Uri.parse(uri);
    request = new Request(downloadUri);
    String filename = downloadUri.getLastPathSegment();
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
    request.setTitle(filename);
    request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    mDownloadDoneCallback = downloadDoneCallback;

    // Set filter to only when download is complete and register broadcast receiver
    IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    context.registerReceiver(downloadReceiver, filter);
    }

    /**
    * Start downloading.
    */
    public void start() {
    if (request == null) {
    return;
    }
    //Enqueue a new download and same the referenceId
    downloadId = downloadManager.enqueue(request);
    }

    /**
    * Get int value of a column for the id.
    *
    * @param id filter id.
    * @param columnName column name.
    * @return first value of the column for the id.
    */
    private int getInt(long id, String columnName) {
    int result = -1;
    Cursor cursor = null;
    try {
    Query query = new Query().setFilterById(id);
    cursor = downloadManager.query(query);
    if (cursor != null && cursor.moveToFirst()) {
    result = cursor.getInt(cursor.getColumnIndex(columnName));
    }
    } finally {
    if (cursor != null) {
    cursor.close();
    }
    }
    return result;
    }

    /**
    * Callback when the download has successfully completed.
    */
    public interface DownloadDoneCallback {
    public void onDownloadDone();
    }
    }