2010-05-12 12 views
2

我有這樣的代碼片段:我如何傳遞一個上下文對象的線程中調用

public static class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> 
{ 
    private final ProgressDialog dialog = new ProgressDialog(ctx); 

    protected void onPreExecute(); 
    protected Boolean doInBackground(final String... args); 
    protected void onPostExecute(final Boolean success); 
} 

正如你看到我用CTX作爲上下文變量我執行這個線程作爲

new ExportDatabaseFileTask().execute(); 

在新的ProgressDialog調用中,如何將上下文傳遞給調用方法?

這一個:

new ExportDatabaseFileTask().execute();* 
+0

爲什麼你會在AsyncTask裏面有ProgressDialog? 我總是把它放在我的活動中,並使用publishProgress – Macarse 2010-05-12 11:43:10

回答

5

我找到了辦法,我只好創建自己的構造函數,而失去靜態的東西

 public ExportDatabaseFileTask(Context ctx) { 
      super(); 
      this.ctx=ctx; 
      dialog= new ProgressDialog(ctx); 
     } 
+0

進行更新yes yes that also works ....我認爲static關鍵字是必需的,而在靜態類中你不能定義構造函數。 – RoflcoptrException 2010-05-12 09:34:15

+0

實際上我需要類名前面的static關鍵字,否則無法識別它,但是我創建了一個新的實例。 – Pentium10 2010-05-12 09:36:45

+0

ty這讓我煩惱了好幾個小時。 – 2011-05-13 23:34:34

1

只要定義一個靜態setter方法,您可以通過上下文對象

0

我剛在開發指南中偶然發現了這一點,我相信這正是它的目的。 android.content.ContextWrapper

相關問題