Android - 从BroadcastReceiver获取标题和描述

时间:2014-01-10 19:01:35

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

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

我正在使用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显示类似...

您能否提供一些如何检索这些信息的提示?

1 个答案:

答案 0 :(得分:1)

  

您能否提供一些如何检索这些信息的提示?

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

This blog post包含说明此过程的示例代码。