Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to finalize a Dialog when the file has finished downloading?
    text
    copied!<p>I was using the method onCreateDialog but was dreprecated.</p> <p>Now, I am using a class to be able to make Dialogs with DialogFragment, but I have problem when closing the Dialog.</p> <p><strong>CustomDialogFragment Class:</strong></p> <pre><code>package com.teste.dialogfragment; import android.app.Dialog; import android.app.DialogFragment; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; public class CustomDialogFragment extends DialogFragment { private ProgressDialog pDialog; public static DialogFragment newInstance(int id) { CustomDialogFragment frag = new CustomDialogFragment(); Bundle args = new Bundle(); args.putInt("dialog_id", id); frag.setArguments(args); return frag; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Log.i("teste RETORNO:", String.valueOf(getDialogId())); switch (getDialogId()) { case 0: // setamos como 0 (horizontal) pDialog = new ProgressDialog(getActivity()); pDialog.setMessage("Wait...downlading file."); pDialog.setIndeterminate(false); pDialog.setMax(100); pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); pDialog.setCancelable(true); pDialog.show(); return pDialog; default: return null; } } @Override public void onCancel(DialogInterface dialog) { // use switch statement as the number of cancellable dialogs increase //if (getDialogId() == Constants.SHOW_LOGIN_DIALOG) { // cancel login here //} pDialog.dismiss();; } public int getDialogId() { return getArguments().getInt("dialog_id"); } </code></pre> <p>}</p> <p>And the Main class.</p> <p>public class Activity_dashboard extends Activity{</p> <pre><code>Button btnShowProgress; public static final int progress_bar_type = 0; private DialogFragment mDialogFragment = null; // files to download private static String file_url_file_01 = "http://www.teste.com.br/teste_01.json"; private static String file_url_file_02 = "http://www.teste.com.br/teste_02.json"; private static String file_url_file_03 = "http://www.teste.com.br/teste_03.json"; private static String file_url_file_04 = "http://www.teste.com.br/teste_04.json"; public static int MY_COUNT = 4; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dashboard); btnShowProgress = (Button) findViewById(R.id.btGetFiles); btnShowProgress.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MY_COUNT = 4; // starting new Async Task new DownloadFileFromURL().execute(file_url_auditar, "teste_01.json"); new DownloadFileFromURL().execute(file_url_hospitais, "teste_02.json"); new DownloadFileFromURL().execute(file_url_pacientes, "teste_03.json"); new DownloadFileFromURL().execute(file_url_login, "teste_04.json"); } }); } /** * Background Async Task * */ class DownloadFileFromURL extends AsyncTask&lt;String, String, String&gt; { @Override protected void onPreExecute() { if(MY_COUNT == 4){ super.onPreExecute(); FragmentManager manager = getFragmentManager(); mDialogFragment = CustomDialogFragment.newInstance(progress_bar_type); mDialogFragment.show(manager, "dialog"); } } @Override protected String doInBackground(String... f_url) { int count; try { URL url = new URL(f_url[0]); URLConnection conection = url.openConnection(); conection.connect(); // this will be useful so that you can show a tipical 0-100% progress bar int lenghtOfFile = conection.getContentLength(); //file name File pathToWrite = new File(Environment.getExternalStorageDirectory() + "/teste/"); File myFile = new File(pathToWrite, f_url[1]); if(!pathToWrite.exists()){ pathToWrite.mkdirs(); } // download the file InputStream input = new BufferedInputStream(url.openStream(), 8192); // Output stream OutputStream output = new FileOutputStream(myFile, false); byte data[] = new byte[1024]; long total = 0; while ((count = input.read(data)) != -1) { total += count; // publishing the progress.... // After this onProgressUpdate will be called publishProgress(""+(int)((total*100)/lenghtOfFile)); // writing data to file output.write(data, 0, count); } // flushing output output.flush(); // closing streams output.close(); input.close(); MY_COUNT--; } catch (Exception e) { Log.e("Error: ", e.getMessage()); } return null; } //protected void onProgressUpdate(Integer... values) { //mDialogFragment.setProgress(values[0]); //} @Override protected void onPostExecute(String file_url) { if(MY_COUNT == 0){ Toast.makeText(getBaseContext(), "Files Updated!", Toast.LENGTH_LONG).show(); getFragmentManager().beginTransaction().remove(mDialogFragment).commitAllowingStateLoss(); mDialogFragment.dismiss(); } } </code></pre> <p>}</p> <p>Look what I call the AsyncTask 4 times because I have to download 4 files. It turns out that opens the Dialog, but when I call OnPostExecute, he closes the Dialog:</p> <pre><code>protected void onPostExecute(String file_url) { if(MY_COUNT == 0){ Toast.makeText(getBaseContext(), "Files Updated!", Toast.LENGTH_LONG).show(); getFragmentManager().beginTransaction().remove(mDialogFragment).commitAllowingStateLoss(); mDialogFragment.dismiss(); } } </code></pre> <p>}</p> <p>It also has the problem, that I could not pass the progress for the progress bar to go as we were updating by downloading</p> <pre><code>protected void onProgressUpdate(Integer... values) { mDialogFragment.setProgress(values[0]); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload