【问题标题】:Android - Get title and description from BroadcastReceiverAndroid - 从 BroadcastReceiver 获取标题和描述
【发布时间】:2026-01-07 10:25:01
【问题描述】:

我正在开发一个安卓应用程序。

我正在使用 BroadcastReciever 来了解下载完成的位置。

        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlString));
    request.setDescription(artistString);
    request.setTitle(titleString);
    // in order for this if to run, you must use the android 3.2 to compile your app
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC + "/Tunesto", titleString + " - " + artistString + ".mp3");

    Toast.makeText(mainContext, "Downloading " + titleString + " - " + artistString, Toast.LENGTH_SHORT).show();

    // get download service and enqueue file
    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request);

然后我在我的 onCreate 方法中使用它来在下载结束时显示祝酒词:

onComplete = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String info = "";
            info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_DESCRIPTION);
            info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_TITLE);
            info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_ID);
            info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_LOCAL_FILENAME);
            info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_MEDIA_TYPE);
            info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
            info = info + " - " + intent.getDataString();
            info = info + " - " + intent.getType();
            info = info + " - " + intent.EXTRA_TITLE;
            info = info + " - " + intent.EXTRA_TEXT;

            //Toast.makeText(mainContext, Integer.valueOf(dlCount) + "  Download \"" + titleString + " - " + artistString + "\" completed", Toast.LENGTH_LONG).show();
            Toast.makeText(mainContext, info + "Download completed", Toast.LENGTH_LONG).show();
        }
     };

    registerReceiver(onComplete, new IntentFilter(
            DownloadManager.ACTION_DOWNLOAD_COMPLETE));

如您所见,我已经尝试了所有方法来获取信息字符串中的描述和标题,但它只会显示 null (tHE toast 显示类似 ...

您能否给我一些关于如何检索这些信息的提示?

【问题讨论】:

    标签: java android eclipse broadcastreceiver android-download-manager


    【解决方案1】:

    您能否给我一些关于如何检索这些信息的提示?

    通过在广播Intent 上调用getLongExtra() 来读取值for EXTRA_DOWNLOAD_ID。然后,将其与DownloadManager.Query 一起使用来检索此下载的数据。在DownloadManager 上调用query() 返回的Cursor 将包含由COLUMN_ 常量键入的值。

    This blog post 有示例代码来说明这个过程。

    【讨论】:

    • 我会试一试并告诉你,即使我现在对此有点困惑