使用DownloadManager和DownloadManager.Query打印成功的下载

时间:2013-10-03 04:44:55

标签: android

我想打印下载程序列出的成功下载文件列表。 下载程序列出了3个成功下载的文件。下面的代码不起作用。代码已在模拟器中运行。

package com.talespinkit.downloadinfodeleter;

import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.database.Cursor;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private DownloadManager downloadManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
     }

    @Override
    protected void onResume() {
        super.onResume();
        Query q = new Query();
        q.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
        Cursor cursor = downloadManager.query(q);
        Log.d(TAG, String.valueOf(cursor.getCount())); // prints 0
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { // Does not enters this loop
            Log.d(TAG, String.valueOf(cursor.getColumnCount()));
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

1 个答案:

答案 0 :(得分:-1)

使用以下命令从SD卡中获取您下载的文件名:

文件sdCard = Environment.getExternalStorageDirectory();

String sdCardPath = sdCard.getAbsolutePath();

文件fileDir =新文件(sdCard.getAbsolutePath()                     +“你的文件名”);