2

URLからファイルをダウンロードしようとしています。ダウンロードが失敗した場合(理由に関係なく)、アプリケーションを1時間後に再試行します。

ダウンロードは(メインスレッドではなく)独自のスレッドで行われるため、新しい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
    }
4

2 に答える 2

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

Downloader onPostExecute()で、1時間で期限切れになる新しいタイマーを開始します。

protected void onPostExecute(File file)
    {
        if (!isVideoLoaded)
          startOneHourTimer();
        else
          //Call your normal UI code here
    }
于 2012-02-10T15:43:45.737 に答える
0

onPostExecuteダウンロードが失敗したことを示すためにメインアクティビティにコールバックするように変更します(ダウンロードが失敗したことを示しMyMainActivity.this.someMethod()ます)。メインアクティビティは新しいAsyncTaskを作成し(古いものを再利用することはできません)、意味します。開始を遅らせる必要があります。

これを行うための1つのオプションは、AsyncTaskにコンストラクターを追加して、遅延パラメーターを渡すことができるようにすることです。その後、AsyncTaskはdoInBackgroundメソッドの一部として遅延パラメーターを使用できます。

doInBackgroundはただ待ってThread.sleep()

于 2012-02-10T05:57:38.683 に答える