Question

I made a program that downloads some files and then plays them. The program cannot play files if they have not been downloaded. I have blocked button play. How do I know when files have been downloaded to unlock the button?

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();
        }
    }
}
Was it helpful?

Solution

you will need to register an BroadcastReceiver for DownloadManager.ACTION_DOWNLOAD_COMPLETE Action and enable button when download complete Action fire from Downloadnamager as:

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

you can see this example for enable/disable button when download complete :

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top