如何使用DownloadManager列

时间:2018-06-01 11:19:52

标签: android android-studio download kotlin android-download-manager

我想知道如何使用像action="options.php"这样的DownloadManager列,因为当我直接使用它时它会给我一个恒定值 我是Android编程的新手(c#background)所以我需要一些关于android的一些基础知识的帮助,我已经阅读了android开发者指南,但它缺少示例,所以它没有价值 我想念c#guide;(
这是我的代码:

COLUMN_ID

Toast文字是:

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()
}

2 个答案:

答案 0 :(得分:1)

收到带有下载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));

        }
    }

答案 1 :(得分:0)

  

这个答案是用Java编写的,你需要修改它。

     

向下载管理员查询已请求的下载。

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);
}