如何使用 DownloadManager 列

how to use DownloadManager columns

我想知道如何使用像 COLUMN_ID 这样的 DownloadManager 列,因为当我直接使用它时它会给我常数值 我是 android 编程新手(c# 背景),所以我需要一些关于 android 的基础知识的帮助,我已经阅读了 android 开发人员指南,但它缺少示例,所以它毫无价值
我想念 C# 指南 ;(
这是我的代码:

fun download(url: String, name: String) {
    //start download request
    var request = DownloadManager.Request(Uri.parse(url))
    request.setVisibleInDownloadsUi(true)
    request.allowScanningByMediaScanner()
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
    request.setDestinationInExternalPublicDir("a destination", "$name.pdf")
    // get download service and enqueue file
    val manager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
    manager.enqueue(request)
    var x = (DownloadManager.COLUMN_ID)
    Toast.makeText(applicationContext,x ,Toast.LENGTH_SHORT).show()
    //or 
    //var x = (DownloadManager.COLUMN_TOTAL_SIZE_BYTES)
    //Toast.makeText(applicationContext,x ,Toast.LENGTH_SHORT).show()
}

Toast 文本是:

_id (for COLUMN_ID)             
total_size(for COLUMN_TOTAL_SIZE_BYTES)

收到带有下载 ID 的广播后,您可以使用该 ID 获取与下载相关的详细信息。在您的广播接收器中,您会得到这样的下载 ID intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID。你拿着这个 id ,查询下载管理器。

    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(downloadId);
    Cursor cursor = downloadManager.query(query);

获得光标后,您可以运行像这样循环。

     if (cursor.moveToFirst()) {
        if (cursor.getCount() > 0) {

            int statusOfTheDownload = cursor.
        getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
            String fileUri = cursor.
        getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

        }
    }

This answer is in Java, you need to modify it.

Query the download manager about downloads that have been requested.

Cursor cursor = downloadManager.query(ImageDownloadQuery);
if (cursor.moveToFirst()) {
    Download(cursor, Image_DownloadId);
}

private void Download(Cursor cursor, long DownloadId) {

        //column for download  columnId
        int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_ID);
        int columnId = cursor.getInt(columnIndex);
        Log.d("Tag", "columnId " +columnId);
}