你好!我有一个活动,它有一个按钮可以从服务器下载一本书。当我单击它时,我想在下载文件时一直显示进度条。总而言之,一个典型的任务。但是加载了几个文件,使用. 而且我不知道如何在加载所有文件时阻止活动中的操作并显示进度条。该库在单独的线程中加载文件。也许我可以直接在 UI 线程中上传文件,而不需要库?那么在加载文件之前,用户肯定不会做任何事情。但这是一个坏主意,恕我直言,如果连接不是很好,它将失败。告诉我,往哪个方向去“挖”,或许有一些例子?
这是我加载文件并写入内存的方法:
private void downloadFilesBook(String[] urlsFiles) {
mFetch = Fetch.newInstance(this);
String folderB = "bookfiles_" + bookId;
String fileNameForWrite = "book_" + bookId + ".json";
File bookfolder = new File(String.valueOf(getExternalFilesDir(folderB)));
ArrayList<String> pagesFiles = new ArrayList<>();
for (int i = 0; i < urlsFiles.length; i++) {
String url = urlsFiles[i];
String path = String.valueOf(bookfolder);
String fileName = Uri.parse(url).getLastPathSegment();
Log.d("my2", fileName);
Request request = new Request(url, path, fileName);
String pageFilePath = path + "/" + fileName;
Log.d("my2", pageFilePath);
pagesFiles.add(pageFilePath);
downloadId = mFetch.enqueue(request);
}
BookFiles bookFiles = new BookFiles();
bookFiles.setBookID(bookId);
ArrayList<String> pagesPath = getPagesArray(pagesFiles);
ArrayList<String> soundsPath = getSoundsArray(pagesFiles);
bookFiles.setPagesPath(pagesPath);
bookFiles.setSoundsPath(soundsPath);
Gson gson11 = new Gson();
String filesJson = gson11.toJson(bookFiles);
MyJSON.saveData(getApplicationContext(), filesJson, fileNameForWrite);
}
有一个用于加载事件的侦听器(这在文档中):
在启动下载之前,在活动顶部显示一个带有进度条(或其他显示下载的内容)的对话框。释放
onCompleted()您的锁定并删除进度条。还值得处理其他状态(错误、暂停、取消、删除等)