我試圖顯示一個不確定的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);
}
}
onCreateDialog()的代碼在哪裏? – Matthias 2010-04-24 08:59:24
Matthias,我添加了onCreateDialog – tedwards 2010-04-24 16:33:50