gpt4 book ai didi

android - 如何在 DownloadManager 工作时阻止程序?

转载 作者:行者123 更新时间:2023-11-29 21:50:30 31 4
gpt4 key购买 nike

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

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

最佳答案

您需要为 DownloadManager.ACTION_DOWNLOAD_COMPLETE 操作注册一个 BroadcastReceiver 并在下载完成时启用按钮 从 Downloadnamager 触发的操作如下:

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

关于android - 如何在 DownloadManager 工作时阻止程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14536347/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com