In the button listener, I launch a stream in which I upload files. After starting the flow, the button text changes from “Download” to “Read”. I didn’t figure out how to do this after the thread / load is complete. Therefore, I put the launch of ProgressDialog on the event - so that the user waited until the download was completed. Otherwise, it receives an error, because the files have not yet been downloaded. Please tell me how to remove ProgressDialog exactly after the download is complete? I load third-party library Fetch

Here is the code for the listener:

buttonDownload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Thread mThread = new Thread(new Runnable() { @Override public void run() { String fileListB = "list_" + "book_" + bookId + ".json"; String jsReadFile = MyJSON.getData(getApplicationContext(), fileListB); Log.d(TAG, jsReadFile); Gson gson = new Gson(); Book book = gson.fromJson(jsReadFile, Book.class); List<String> pages = book.getPageUrl(); List<String> sounds = book.getSoundUrl(); String[] urlsPages = pages.toArray(new String[0]); String[] urlsSounds = sounds.toArray(new String[0]); String[] urlsFiles = ArrayAndArrayNewArray(urlsPages, urlsSounds); DownloadFilesBook(urlsFiles); } }); mThread.start(); // запустили поток 2 ProgressDialog progressDialog = new ProgressDialog(BookCardActivity.this); progressDialog.setMessage(getString(R.string.progressDialogText)); progressDialog.show(); buttonDownload.setText(R.string.buttonRead); buttonDownload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NextActivity(); } }); } }); } else { buttonDownload.setText(R.string.buttonRead); buttonDownload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NextActivity(); } }); } 

Here is the code for the file upload method.

 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); } 
  • the dismiss () method of the dialog. but generally show progress by the dialogue practice is not very - Sviat Volkov
  • dismiss () will remove it instantly, without waiting for the download to complete - Vyacheslav
  • so call it after the download is over) - Sviat Volkov
  • This is a classic work with streams, nobody will answer you until you show what DownloadFilesBook is doing (urlsFiles); and why is it capitalized? Is this a class of some kind, like Async? or is it a method. If it is in a separate thread, it still creates an asynchronous task, then everything is more complicated, if it does not produce threads inside it, then it is a little simpler - Shwarz Andrei
  • By the way, ProgressDialog bad practice, from Google and it is already deprecated in some API, it is advised to use the ProgressBar, well, it is in between times - Shwarz Andrei

2 answers 2

I have never worked with fetch , but if you look at the documentation, there are methods for tracking the download process.

To do this, you must assign a handler to mFetch

Example

 mFetch.addListener(new FetchListener() { @Override public void onQueued(@NotNull Download download) { .... } @Override public void onCompleted(@NotNull Download download) { //здесь обработайте завершение загрузки (dismissу диалога, например) } ..... 

Documentation

  • Yes, I watched the documentation, but for some reason it does not pick up the event, or I do it wrong ... - Vyacheslav
  • @ Vyacheslav need to watch your attempts to implement this listener. Add them to the question. - Likhanov
  • Yes, it looks like an answer, and most likely it is done in a separate thread, then the author does not need to create a Thread, but just hang the listener. - SweetDev
  • unfortunately, while writing my application, the third-party library has been updated ... now you need to redo all the logic of loading - Vyacheslav

I would suggest using AsyncTask - this is a good practice in Android . Transfer code from Thread.run() to AsyncTask.doInBackground() - the method runs in a separate thread. And in AsyncTask.onPostExecute() remove ProgressDialog - this method is executed in the ui-stream. The display of the dialog can be transferred to AsyncTask.onPreExecute() - this one is also in the ui-stream.

  • I use a third-party library in my method (see above), which probably uses threads to load a file queue, and an overlay using AsyncTask is possible here - Vyacheslav
  • one
    I would not say that asyncTask is a good practice for network operations - Likhanov