0

我正在研究android應用程序。Android - 從BroadcastReceiver獲取標題和描述

我正在使用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)); 

正如你所看到的,我想盡一切辦法獲取信息字符串中的說明和標題,但它不會顯示沒有別的比空(祝酒顯示類似...

能否請您給我如何獲取這些信息顯示提示?

回答

1

您可以給我一些關於如何檢索這些信息的提示嗎?

閱讀的價值for EXTRA_DOWNLOAD_ID,通過在廣播Intent調用getLongExtra()。然後,使用DownloadManager.Query來檢索此下載的數據。您撥打query()返回的Cursor將包含由COLUMN_常量鍵入的值。

This blog post有說明此過程的示例代碼。

+0

我會試試這個,讓你知道,即使我現在有點困惑 – user3119384

+0

工作過,謝謝! – user3119384