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

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;
    } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图