Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best approach to do this is by using the AsyncTask class, as it will allow you to execute some background process and update the UI at the same time (in your case, it's a progress bar).</p> <p>This is an example code:</p> <pre><code>ProgressDialog mProgressDialog = new ProgressDialog(YourActivity.this); mProgressDialog.setMessage("A message"); mProgressDialog.setIndeterminate(false); mProgressDialog.setMax(100); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); DownloadFile downloadFile = new DownloadFile(); downloadFile.execute("the url to the file you want to download"); </code></pre> <p>The AsyncTask will look like this:</p> <pre><code>private class DownloadFile extends AsyncTask&lt;String, Integer, String&gt;{ @Override protected String doInBackground(String... url) { int count; try { URL url = new URL(url[0]); URLConnection conexion = url.openConnection(); conexion.connect(); // this will be useful so that you can show a tipical 0-100% progress bar int lenghtOfFile = conexion.getContentLength(); // downlod the file InputStream input = new BufferedInputStream(url.openStream()); OutputStream output = new FileOutputStream("/sdcard/somewhere/nameofthefile.ext"); byte data[] = new byte[1024]; long total = 0; while ((count = input.read(data)) != -1) { total += count; // publishing the progress.... publishProgress((int)(total*100/lenghtOfFile)); output.write(data, 0, count); } output.flush(); output.close(); input.close(); } catch (Exception e) {} return null; } </code></pre> <p>The method above (doInBackground) runs always on a background thread. You shouldn't do any UI tasks there. On the other hand, the onProgressUpdate runs on the UI thread, so there you will change the progress bar:</p> <pre><code>@Override public void onProgressUpdate(String... args){ // here you will have to update the progressbar // with something like mProgressDialog.setProgress(args[0]); } </code></pre> <p>} You will also want to override the onPostExecute method if you want to execute some code once the file has been downloaded completely.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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