获取当前已完成下载

时间:2014-04-10 04:44:15

标签: android android-download-manager

即时通讯使用DownloadManager下载android中的文件,我需要为每个下载的文件运行一个命令,但我的接收器无法正常工作,如果我下载10个文件,它只返回最后下载的文件标题,为什么?

BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {

            long downloadId = intent.getLongExtra(
                    DownloadManager.EXTRA_DOWNLOAD_ID, 0);
            Query query = new Query();
            query.setFilterById(enqueue);
            Cursor c = dmAndroid.query(query);
            if (c.moveToFirst()) {
                //Log.d("ainfo", c.getString(c.getColumnIndex("title"))); -> stil return just the last
                /*
                int i = 0;
                 while(i < c.getColumnCount()) { Log.d("ainfo",
                 c.getColumnName(i) + "-" + i + "-" + c.getString(i));
                 i++; }
                */
                int columnIndex = c
                        .getColumnIndex(DownloadManager.COLUMN_STATUS);

                if (DownloadManager.STATUS_SUCCESSFUL == c
                        .getInt(columnIndex)) {

// if i put here still return the last download only
                    String uriString = c
                            .getString(c
                                    .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

                    if (downloadId == c.getInt(0)) {
                        Log.d("ainfo", c.getString(c.getColumnIndex("title"))); //same, just the last download file title


                    }
                }


            }

        }
    }
};

1 个答案:

答案 0 :(得分:0)

这可能是原因 您是否在Android清单文件中注册了广播接收器? 你在logcat上发现任何错误