2010-04-24 101 views
1

我試圖顯示一個不確定的ProgressDialog,而AsyncTask綁定到RemoteService。當服務第一次創建時,RemoteService會建立用戶聯繫人列表。對於一長串聯繫人,這可能需要5到10秒。ProgressDialog直到AsyncTask完成後才顯示

我遇到的問題是ProgressDialog在RemoteService建立它的聯繫人列表之後才顯示。我甚至嘗試過放入Thread.sleep來讓ProgressDialog時間顯示出來。通過sleep語句,ProgressDialog加載並開始旋轉,但只要RemoteService開始工作就會鎖定。

如果我只是把AsyncTask變成虛擬代碼,讓它休眠一段時間,一切正常。但是,當任務需要做實際工作時,就像UI剛剛坐下來等待。

關於我在做什麼錯誤的任何想法?

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.d(IM,"Start Me UP!!"); 
    setContentView(R.layout.main); 
    Log.d(IM, "Building List View for Contacts"); 
    restoreMe(); 
    if (myContacts==null){ 
     myContacts = new ArrayList<Contact>(); 
     this.contactAdapter = new ContactAdapter(this, 
                R.layout.contactlist, 
                myContacts); 
     setListAdapter(this.contactAdapter); 
     new BindAsync().execute(); 
    } 
    else{ 
     this.contactAdapter = new ContactAdapter(this, 
                R.layout.contactlist, 
                myContacts); 
     setListAdapter(this.contactAdapter); 

    } 
} 

private class BindAsync extends AsyncTask<Void, Void, RemoteServiceConnection>{ 
    @Override 
    protected void onPreExecute(){ 
     super.onPreExecute(); 
     Log.d(IM,"Showing Dialog"); 
     showDialog(DIALOG_CONTACTS); 
    } 
    @Override 
    protected RemoteServiceConnection doInBackground(Void... v) { 
     Log.d(IM,"Binding to service in BindAsync"); 
     try{ 
     Thread.sleep(2000); 
     } catch (InterruptedException e){ 

     } 

     RemoteServiceConnection myCon; 
     myCon = new RemoteServiceConnection(); 
     Intent i = new Intent(imandroid.this,MyRemoteService.class); 
     bindService(i, myCon, Context.BIND_AUTO_CREATE); 
     startService(i); 
     Log.d(IM,"Bound to remote service"); 
     return myCon; 
    } 
    @Override 
    protected void onPostExecute(RemoteServiceConnection newConn){ 
     super.onPostExecute(newConn); 
     Log.d(IM,"Storing remote connection"); 
     conn=newConn; 
    } 

}; 

編輯:添加onCreateDialog

protected Dialog onCreateDialog(int id){ 
    switch(id){ 
    case DIALOG_CONTACTS: 
     ProgressDialog progDialog = new ProgressDialog(imandroid.this); 
     progDialog.setMessage("Loading Contacts... Please Wait"); 
     progDialog.setCancelable(false); 
     return progDialog; 
    default: 
     return super.onCreateDialog(id); 
    } 
} 
+1

onCreateDialog()的代碼在哪裏? – Matthias 2010-04-24 08:59:24

+0

Matthias,我添加了onCreateDialog – tedwards 2010-04-24 16:33:50

回答

0

不要從doInBackground()bindService()。首先,它幾乎是瞬間的,所以你不需要在後臺線程中使用它 - 你所做的只是浪費CPU時間和電池。其次,它需要與Looper和消息隊列Context,因此把它放在後臺線程是危險的恕我直言。

另請注意,您都綁定到服務並啓動服務。有幾種情況適用,但通常您只需要一種或另一種。

+0

這就是事情,它不是瞬時的。我需要該服務來構建並保留用戶聯繫人列表。所以我有服務在onCreate中建立聯繫人。然後,我綁定它後,我從服務中獲取聯繫人並顯示它們。 我不認爲會有任何併發​​問題,因爲AsyncTask綁定到RemoteService後退出。也許有,並阻止了UI線程? 我綁定並啓動服務,因爲我計劃讓多個應用程序綁定到該服務。這不合適嗎? – tedwards 2010-04-24 16:55:03

+0

「所以我有服務建立它的onCreate中的聯繫人。」將* that *邏輯移入服務中的AsyncTask,因爲在主應用程序線程上調用onCreate(),無論您嘗試綁定到哪個位置。 – CommonsWare 2010-04-24 17:18:05

+0

我也嘗試過類似的方法,但是我遇到了必須等待聯繫生成線程加入的問題,然後才能將調用的結果返回給RemoteServiceConnection所使用的線程。 聽起來就像我需要弄清楚如何將消息傳遞迴UI線程,讓它知道聯繫人何時可以使用。 感謝您的幫助! 順便說一下,我正在考慮購買你的書。這筆交易的簽署(以及發生的時間)。 – tedwards 2010-04-24 17:47:54

相關問題