我想显示无限期ProgressDialog,而一个的AsyncTask结合到RemoteService。 该RemoteService建立时,第一次创建该服务的用户的联系人列表。 对于接触的一个长长的清单,这可能需要5〜10秒。
我遇到的问题,就是ProgressDialog不显示,直到后RemoteService已建成它的联系人列表。 我甚至尝试把一个在了Thread.sleep给予ProgressDialog时间露面。 随着睡眠声明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);
}
}