1

我制作了一个下载一些文件然后播放它们的程序。如果文件尚未下载,程序将无法播放文件。我阻止了按钮播放。我如何知道何时下载了文件以解锁按钮?

private DownloadManager manager;

public void downloadFiles() {
    ArrayList<HashMap<String, String>> pl = getPlayList();
    ArrayList<String> fileListForDownload = xm.getDownloadList(pl);
    for (int i=0; i<fileListForDownload.size(); i++) {
        Log.i("tag", fileListForDownload.get(i));
        String url = BASE_URL + fileListForDownload.get(i);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setDescription(fileListForDownload.get(i));
        request.setTitle(fileListForDownload.get(i));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }
        request.setDestinationInExternalPublicDir(DIRECTORY_FOR_MUSIC, fileListForDownload.get(i));
        // get download service and enqueue file
        try {
            downloadReference = manager.enqueue(request);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
4

1 回答 1

2

从 Downloadnamager 下载完整的 Action 触发时,您需要注册一个BroadcastReceiverfor Action 和 enable 按钮:DownloadManager.ACTION_DOWNLOAD_COMPLETE

findViewById(R.id.play).setEnabled(false); //<< disable button here
DownloadManager mgr=(DownloadManager)getSystemService(DOWNLOAD_SERVICE);

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

 BroadcastReceiver onComplete=new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {
      findViewById(R.id.play).setEnabled(true);  //<< enable button here
    }
  };

下载完成后,您可以看到启用/禁用按钮的此示例:

https://github.com/commonsguy/cw-android/blob/master/Internet/Download/src/com/commonsware/android/download/DownloadDemo.java

于 2013-01-26T11:18:34.717 回答