2012-02-10 10 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 클래스에서 는 1 시간에 만료됩니다 새로운 타이머를 시작 ) 당신의 다운 onPostExecute (에서이 기능을

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(); 
} 

를 사용합니다.

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

당신이 (당신이 할 수있는 (MyMainActivity.this.someMethod()하여 얻을 수 있어야합니다.)의 주요 활동은 다음 새로운 AsyncTask를 만들 수 있습니다 다운로드가 실패했음을 나타 내기 위해 다시 주요 활동으로 부른다 너무 onPostExecute 이전 것을 재사용하지 마십시오) 시작이 지연되어야 함을 나타냅니다.

AsyncTask에 지연 매개 변수를 전달할 수 있도록 AsyncTask에 생성자를 추가하면 AsyncTask에서 doInBackground 메서드의 일부로 지연 매개 변수를 사용할 수 있습니다. doInBackground 내부

은 단순히 당신은 당신의 UI 스레드에서 다운로드를 시작하는 타이머를 만들 수 Thread.sleep()