wqb1990 2020-07-06 19:11 采纳率: 0%
浏览 670
已结题

android自带的DownloadManager如何实现断点续传下载功能?

android自带的DownloadManager如何实现断点续传下载功能?

  • 写回答

2条回答 默认 最新

  • Dewbie 2020-07-06 22:54
    关注
    is it possible to get somehow this http code?
    
    Currently, no. The DownloadManager reports a STATUS_SUCCESSFUL even when a download failed, for example because the url/file was not found (HTTP 404) (this is a bug).
    Also see DownloadManager sends STATUS_SUCCESSFUL for failed download
    
    I know it's a relatively old thread but that issue still remains. I tested it 5 minutes ago and it still doesn't work properly.
    
    is it possible to listen to "download pause" event without querying constantly the download manager?
    
    Weirdly, no. The only available "events" to listen for are: enter image description here
    
    To get around this, you have to query the status yourself every X time by checking
    
    if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_PAUSED) {
        // Do stuff
    }
    is download manager (on API level 16+) supports https (ssl) ?
    
    It used to not support https (read more in Android DownloadManager and SSL (https)), however it does now. You can simply verify by trying to retrieve a file from a https origin, for example https://mdn.mozillademos.org/files/3794/mixed_content_webconsole.jpg. You will see that it retrieves the file fine.
    
    what exactly is download manager retry policy? can I change it default retry policy?
    
    It is currently not possible to change the retry 'policy'. See the docs and you will find there are no methods or properties regarding this functionality.
    
    Regarding the default retry policy, useful information can be found in the following package: com.android.providers.downloads.Constants. This links to the 5.1.1 version, if you need the information for another version you can manually navigate to that. For example here is the information for android 4.0.1 (the retry policy values are the same as in 5.1.1).
    
    It states:
    
    The number of times that the download manager will retry its network operations when no progress is happening before it gives up.
    
    public static final int MAX_RETRIES = 5;
    The minimum amount of time that the download manager accepts for a Retry-After response header with a parameter in delta-seconds.
    
    public static final int MIN_RETRY_AFTER = 30; // 30s
    The maximum amount of time that the download manager accepts for a Retry-After response header with a parameter in delta-seconds.
    
    public static final int MAX_RETRY_AFTER = 24 * 60 * 60; // 24h
    As you might have guessed, these are final (constant) and thus cannot be changed.
    
    Conclusion: DownloadManager is pretty useful to do some basic downloading, but it's functionality is pretty limited.
    I can suggest an alternative: there's a downloadmanager in the android-common libary over at https://github.com/Trinea/android-common 
    

    很不幸地告诉你,DownloadManager几乎无法实现这个功能。
    官方建议使用这个库 https://github.com/Trinea/android-common

    硬性解:

    private boolean resumeDownload(Context context, String downloadTitle) {
        int updatedRows = 0;        
    
        ContentValues resumeDownload = new ContentValues();
        resumeDownload.put("control", 0); // Resume Control Value
    
        try {
            updatedRows = context
                .getContentResolver()
                .update(Uri.parse("content://downloads/my_downloads"),
                    resumeDownload,
                    "title=?",
                    new String[]{ downloadTitle });
        } catch (Exception e) {
            Log.e(TAG, "Failed to update control for downloading video");
        }
    
        return 0 < updatedRows;
    }
    
    private boolean pauseDownload(Context context, String downloadTitle) {
        int updatedRows = 0;
    
        ContentValues pauseDownload = new ContentValues();
        pauseDownload.put("control", 1); // Pause Control Value
    
        try {
            updatedRows = context
                .getContentResolver()
                .update(Uri.parse("content://downloads/my_downloads"),
                    pauseDownload,
                    "title=?",
                    new String[]{ downloadTitle });
        } catch (Exception e) {
            Log.e(TAG, "Failed to update control for downloading video");
        }
    
        return 0 < updatedRows;
    } 
    
    评论

报告相同问题?