2013-09-23 85 views
0

我是Android新手,我不知道如何做一些事情。我的應用程序有一個活動,在onCreate函數中,我需要從遠程服務器獲取一些異步數據並對其進行處理。我有一個不同的類,一個服務,用於檢索數據並完成相關工作。我也想告訴用戶工作進度,所以我正在實施委託模式。Android上委託模式的問題

所以,我的活動是這樣的:

public class HomeActivity extends FragmentActivity implements UpdateLibraryDelegate{ 

     .... 

     protected void onCreate(Bundle savedInstanceState) { 

     .... 

     UpdateLibraryTask updateLibraryTask = new UpdateLibraryTask(); 
     updateLibraryTask.setUpdateLibrarayDelegate(this); 
     updateLibraryTask.execute(); 

    } 

    private class UpdateLibraryTask extends AsyncTask<Void, Void, Void>{ 

    private UpdateLibraryDelegate updateLibraryDelegate; 
    @Override 
    protected Void doInBackground(Void... params) { 
     myService.setUpdateLibraryDelegate(updateLibraryDelegate); 
     myService.updateLibrary(); 
     return null; 
    } 

    public void setUpdateLibrarayDelegate(UpdateLibraryDelegate updateLibraryDelegate){ 
     this.updateLibraryDelegate = updateLibraryDelegate; 
    } 

} 

/**DELEGATION METHODS**/ 
@Override 
public void startDownloadingLibrary() {   
... 
} 
@Override 
public void endDownloadingLibrary() {   
... 
} 
@Override 
public void processingLibrary(final int progress) { 
} 

我的服務有這樣的事情:

public void updateLibrary(){ 
    ... 
    if(updateLibraryDelegate!=null){ 
     updateLibraryDelegate.startDownloadingLibrary(); 
    } 
    get(library_client, url, null, library_response_handler); 
} 
public JsonHttpResponseHandler library_response_handler = new JsonHttpResponseHandler() { 
    @Override 
    public void onSuccess(JSONObject jsonObject) 
    { 
     if(updateLibraryDelegate!=null){ 
      updateLibraryDelegate.endDownloadingLibrary(); 
     } 
      for(...){ 
       //PROCESSING DATA 
       updateLibraryDelegate.processingLibrary(process); 
      } 
    } 
} 

有了這些代碼的地方,它似乎做工精細,委託函數得到在他們應該時打電話。當我嘗試在UI上顯示任何內容時,問題就來了。如果我試試這個:

@Override 
    public void startDownloadingLibrary() { 
     if(progressDialog==null){ 
      progressDialog = new ProgressDialog(ctx); 
     } 

     progressDialog.setProgress(ProgressDialog.STYLE_SPINNER); 
     progressDialog.setMessage("Downloading Stories, This may take a while..."); 

     progressDialog.show(); 
} 

我得到一個異常:

09-23 14:08:00.850: E/AndroidRuntime(11336): FATAL EXCEPTION: AsyncTask #1 
09-23 14:08:00.850: E/AndroidRuntime(11336): java.lang.RuntimeException: An error occured while executing doInBackground() 
09-23 14:08:00.850: E/AndroidRuntime(11336): at android.os.AsyncTask$3.done(AsyncTask.java:299) 
09-23 14:08:00.850: E/AndroidRuntime(11336): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 
09-23 14:08:00.850: E/AndroidRuntime(11336): at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 
09-23 14:08:00.850: E/AndroidRuntime(11336): at java.util.concurrent.FutureTask.run(FutureTask.java:239) 
09-23 14:08:00.850: E/AndroidRuntime(11336): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
09-23 14:08:00.850: E/AndroidRuntime(11336): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
09-23 14:08:00.850: E/AndroidRuntime(11336): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
09-23 14:08:00.850: E/AndroidRuntime(11336): at java.lang.Thread.run(Thread.java:841) 
09-23 14:08:00.850: E/AndroidRuntime(11336): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
09-23 14:08:00.850: E/AndroidRuntime(11336): at android.os.Handler.<init>(Handler.java:197) 

我發現,這是因爲我上運行一個線程,不是UI線程的方法,並將該溶液這樣的:

public void startDownloadingLibrary() { 
     new Thread(){ 
      public void run(){ 
       HomeActivity.this.runOnUiThread(new Runnable(){ 

        @Override 
        public void run() { 
          if(progressDialog==null){ 
           progressDialog = new ProgressDialog(ctx); 
          } 

           progressDialog.setProgress(ProgressDialog.STYLE_SPINNER); 
           progressDialog.setMessage("Downloading Stories, This may take a while..."); 

          progressDialog.show(); 

        } 

       }); 
      } 
     }.start(); 
} 

但如我所料,這並不工作,但委託的功能越來越被稱爲權,代碼run方法中它不是直到DAT的處理執行a完成了。

什麼是正確的方式告知用戶有關異步過程的進度?

有沒有什麼辦法可以在不使用runOnUiThread函數的情況下在我的代理函數上顯示對話框?

我將不勝感激任何幫助...

謝謝!

+0

爲什麼你在startDownloading中創建另一個線程,從HomeActivity.this.runOnUiThread開始... –

回答