2012-02-10 61 views
1

我试图从URL下载文件。如果下载失败(无论原因如何),我希望应用程序在一个小时后重试。当我不在主线程中时如何运行CountDownTimer

由于下载是在它自己的线程(而不是主线程)中,我似乎无法启动一个新的CountDownTimer。

我不想阻塞下载线程,所以我试图使用CountDownTimer。

是否有另一种方法可以做到这一点?

在此先感谢!

private class Downloader extends AsyncTask<Object, Void, File> 
{ 

    @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 
    } 

回答

1

等待您可以创建一个定时器,将开始在你的UI线程下载。 在你的UI类,使用此功能

private StartOneHourTimer() 
{ 
new CountDownTimer(10000 * 3600, 1000) { 
    public void onTick(long millisUntilFinished) { 
     mTextField.setText("seconds remaining: " + millisUntilFinished/1000); 
    } 
    public void onFinish() { 
     m_downloader = new Downloader(..); 
     m_downloader.execute(..); 
    } 
    }.start(); 
} 

在你下载onPostExecute(), 开始新的定时器,将在1小时内到期。

protected void onPostExecute(File file) 
    { 
     if (!isVideoLoaded) 
      startOneHourTimer(); 
     else 
      //Call your normal UI code here 
    } 
0

更改onPostExecute,以便它回调到主活动,以表明下载失败(应该能够MyMainActivity.this.someMethod()得到它。)然后,主要活动可以使一个新的AsyncTask(你可以不重用旧的),并表示它应该延迟开始。

这样做的一个选择是向AsyncTask添加构造函数,以便您可以传递延迟参数,然后您的AsyncTask可以使用延迟参数作为doInBackground方法的一部分。

doInBackground简单地用Thread.sleep()

相关问题