2011-08-09 29 views
3

私はアドバイスが必要ですこの解決策は、オーバーフローの原因ではない、私はAsyncTaskで読み取ったデータを更新し、AsyncTaskの終了後に何度も更新する必要があります。この解決策は)初めてコール新しいDownloadFilesTask(私はアドバイスが必要です新しいAsyncTask再帰呼び出し

private class DownloadFilesTask extends AsyncTask<URL,Integer,com.ring_view.www.json.System> { 

    @Override 
    protected com.ring_view.www.json.System doInBackground(URL... params) { 
     int count = params.length; 
     URL temp=params[0]; 
     System system=null; 
     try { 
      system = Communicator.getSystem(temp); 
     } catch (LoggingConnectionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (JSONParsingErrorException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return system; 
    } 

    protected void onProgressUpdate(Integer... progress) { 
     //setProgressPercent(progress[0]); 
    } 

    protected void onPostExecute(com.ring_view.www.json.System result) { 
     txtWorkAllowedValue.setText(result.work_allowed); 
     try { 
      new DownloadFilesTask().execute(new URL("http://test/status-system.json")); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

} 

許容し、安全である(新URL( "のhttp://test/status-system.jsonを"))を実行します。; OvCreateメソッドでそれはエミュレータで正常に動作します。これは安全か、もっと洗練された解決策がありますか?

答えて

5

はい、AsyncTaskを複数回インスタンス化することは、例えば、許容され...

new DownloadFilesTask().execute(...); 
... 
new DownloadFilesTask().execute(...); 

が...許可されています。

あなたはしかし、次のような何かをしてはならない...

DownloadFilesTask myTask = new DownloadFilesTask(); 
myTask.execute(...); // This is OK 
myTask.execute(...); // This will cause an exception 

二度同じスレッドを実行することが法的ではないためです。 newを使用する最初の例では、doInBackground(...)の新しいスレッドを繰り返し作成しますが、2番目の例では、前のスレッドを再利用しようとしています。

1

デフォルトでは、AsyncTaskは自身のオブジェクトプールを自動的に処理します。だからあなたはあふれることを心配する必要はありません。私はそれが10 AsyncTasksがデフォルトでいつでも実行することができると思う、正確な数がわからない。そして、はい、MisterSquonkのように毎回新しい仕事を作らなければならないと言いました。

0

インデックスや条件のように、停止させるには何かが必要です。永遠に実行されることになります。

私は次のようでした:

private ProgressDialog mProgressDialog; 

private class FilesDownloadTask extends AsyncTask<Integer, Integer, Integer> { 

    private Context context; 

    public FilesDownloadTask(Context context) { 
     this.context = context; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     if (mProgressDialog == null) { 
      mProgressDialog = new ProgressDialog(context); 
      mProgressDialog.setMessage(getString(R.string.downloading)); 
      mProgressDialog.setIndeterminate(true); 
      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      mProgressDialog.setCancelable(false); 
      mProgressDialog.show(); 
     } 
    } 

    @Override 
    protected Integer doInBackground(Integer... index) { 

     int i = index[0]; 

     if (i < fileList.length) { 
      if (!new File(path[i]).exists()) 
       doDownload(urls[i], path[i]); 
     publishProgress(i); 
     } 

     return i++; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) { 
     super.onProgressUpdate(progress); 
     mProgressDialog.setIndeterminate(false); 
     mProgressDialog.setMax(fileList.length); 
     mProgressDialog.setProgress(progress[0]); 
    } 

    @Override 
    protected void onPostExecute(Integer nextIndex) { 
     if (index < fileList.length) 
      new FilesDownloadTask(context).execute((Integer) nextIndex); 
     else 
      mProgressDialog.dismiss(); 
    } 

    @Override 
    protected void onCancelled() { 
     mProgressDialog.dismiss(); 
     super.onCancelled(); 
    } 
} 
関連する問題