Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to run CountDownTimer when I'm not in the main thread
    primarykey
    data
    text
    <p>I am trying to download a file from a URL. If the download fails (regardless of the reason) I want the application to retry an hour later.</p> <p>Since the download is in it's own thread (not the main thread), I can't seem to start a new CountDownTimer. </p> <p>I don't want to block the download thread so I'm trying to use the CountDownTimer.</p> <p>Is there another way to do this?</p> <p>Thanks in advance!</p> <pre><code>private class Downloader extends AsyncTask&lt;Object, Void, File&gt; { @Override protected File doInBackground(Object... params) { Context context = (Context) params[0]; String strUrl = (String) params[1]; String strFileName = (String) params[2]; return download(context, strUrl, strFileName); } /** * Downloads files in a separate thread. Adds notification of download to status bar. */ public File download(final Context context, final String url, final String fileName) { boolean isVideoLoaded=false; try { URL urlObj = new URL(url); URLConnection con = urlObj.openConnection(); BufferedInputStream bis = new BufferedInputStream(con.getInputStream(), BUFFER_SIZE); FileOutputStream fos = new FileOutputStream(retFile); byte[] bArray = new byte[BUFFER_SIZE]; int current = 0; int read = 0; while(current != -1) { fos.write(bArray,0,current); current = bis.read(bArray, 0, BUFFER_SIZE); read = read + current; } fos.close(); bis.close(); isVideoLoaded = true; strFileName = retFile.getAbsolutePath(); } catch(Exception ioe) { Log.d("Downloader.download", "Error: " + ioe.toString(), ioe); } } if (!isVideoLoaded) // if video is still not loaded { // sleep then try again new CountDownTimer(15000, 2000) { public void onFinish() { Downloader dh = new Downloader(); Object params[] = new Object[3]; params[0] = context; params[1] = url; params[2] = fileName;*/ dh.execute(params); } @Override public void onTick(long millisUntilFinished) { // TODO Auto-generated method stub } }.start(); } return retFile; } protected void onPostExecute(File file) { // display downloaded file } </code></pre>
    singulars
    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. This table or related slice is empty.
    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