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 は特別な許可を求めますか?

4

1 に答える 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; 
                }
            }

ダウンロードを成功させるには、インターネット接続、デバイス ストレージの空き容量なども必要です。とにかく、ダウンロード マネージャーはダウンロード要求に応じて、ユーザーにその結果を説明する必要があります。

于 2013-03-01T16:25:37.863 に答える