使用DownloadManager,我如何查看设备上所有活动/正在运行的下载?

3

如何使用DownloadManager查看设备上所有正在进行的下载?

我的代码:

            DownloadManager.Query query = null;
        Cursor c = null;
        DownloadManager downloadManager = null;
        downloadManager = (DownloadManager)m_context.getSystemService(Context.DOWNLOAD_SERVICE);
        query = new DownloadManager.Query();
         if(query!=null) 
         {
                    query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PAUSED|DownloadManager.STATUS_SUCCESSFUL|
                            DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_PENDING);
         } 
        c = downloadManager.query(query);
        if(c.moveToFirst()) 
        { 
            int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); 
            switch(status) 
            { 
            case DownloadManager.STATUS_PAUSED: 
            break; 
            case DownloadManager.STATUS_PENDING: 
            break; 
            case DownloadManager.STATUS_RUNNING: 
            break; 
            case DownloadManager.STATUS_SUCCESSFUL: 
            break; 
            case DownloadManager.STATUS_FAILED: 
            break; 
            }
        }   

在c.moveToFirst()函数上失败了(返回false);

DownloadManager是否需要一些特殊的权限?

1个回答

2

DownloadManager 的文档中指出:

请注意,应用程序必须具有 INTERNET 权限才能使用此类。

我的测试应用程序只有 android.permission.INTERNET 权限,并且可以成功添加下载请求。

            // FILL request object
            Uri uri =  Uri.parse( "http://mysite.com/myfile");
            DownloadManager.Request request=new DownloadManager.Request(uri);
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                    .setAllowedOverRoaming(false)
                    .setTitle("my title")
                    .setDescription("my description")
                    ;

            // ADD request to download manager
            DownloadManager dm=(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
            long id = dm.enqueue(request);

            // CHECK request by id
            Cursor c = dm.query( new DownloadManager.Query().setFilterById(id) );
            if( c.moveToFirst() ){
                int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); 
                switch(status) 
                { 
                case DownloadManager.STATUS_PAUSED:
                break; 
                case DownloadManager.STATUS_PENDING:
                break; 
                case DownloadManager.STATUS_RUNNING:
                break; 
                case DownloadManager.STATUS_SUCCESSFUL:
                break; 
                case DownloadManager.STATUS_FAILED:
                break; 
                }
            }

要成功下载,还需要有网络连接、设备存储空间等条件。不管怎样,下载管理器应该尽力满足下载请求,并向用户解释其结果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接