這是我的拳頭stackoverflow後,所以請溫柔與我!我敢肯定,我正在嘗試做的事情是可能的,這是我做過的事情(或沒有完成?),這是造成這個問題...我只是不知道什麼東西是。Android ProgressDialog不會旋轉
我想要做的事:
顯示ProgressDialog而我的應用程序synchronises並處理下載的數據。
問題:
的ProgressDialog節目,但沒有轉動(這使得它看起來像它已凍結),同步和處理情況發生,ProgressDialog關閉,應用程序繼續正常。
如何我目前特林做到這一點:
創建ProgressDialog - 在我的活動
執行同步 - 在我的服務
過程中的數據 - 在我的服務
Dismis的ProgressDialog - 在我活動
我已經試過的東西:
使用線程(下面的代碼) 使用AsynTask(如果需要的話可以提供代碼) 使用處理程序(可以提供代碼,如果需要)
花費大量時間尋找我的問題的答案後,似乎其他人有相同或類似的問題,並設法解決它使用上述想法之一。但是,我一直無法實現任何這些想法來解決我的特定問題。這使我確信這是我做錯了什麼......我只是不知道是什麼。
原代碼我寫的和我使用作爲我的所有嘗試的修復的基礎:
首先
public class myActivity extends ListActivity {
private ProgressDialog dialog;
private boolean mIsBound;
private myService mBoundService;
private ServiceConnection mConnection;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*if we need to login start login activity for result*/
...
}
...
/*other methods*/
...
}
然後
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode){
case ACTIVITY_LOGIN:
/*after login we know we always need to sync*/
dialog = new ProgressDialog(myActivity.this);
dialog.setMessage("Synchronising...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
Thread doBind = new Thread(new Runnable(){public void run(){doBindService();}});
doBind.start();
break;
}
}
所以現在我假設doBind正在發生在另一個線程離開UI線程除了顯示ProgressDialogue之外什麼也不做...?
private boolean doBindService() {
mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((myService.LocalBinder)service).getService();
while (mBoundService.isRunning()){
/*wait for service to finish before doing anything else... myMethod() is expecting the sync to have completed*/
/*I suspect this loop to be the thing causing the problem. But I'm not sure why because it is in a different thread so shouldn't interfear with the dialog?! And if that is what is causing the problem then how else can I do this?*/
}
/*get the activity to do Stuff with the downloaded data*/
myMethod();
/*finished with the service for now so unbind*/
doUnbindService();
if (dialog != null) {
dialog.dismiss();
}
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
boolean myReturn = bindService(new Intent(myActivity.this, myService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = myReturn;
return myReturn;
}
private void doUnbindService() {
if (mIsBound) {
unbindService(mConnection);
mIsBound = false;
}
}
請讓我知道如果您需要任何進一步的信息,以幫助我解決這個問題。
編輯:
這裏是我使用的使用處理,而不是代碼。這與以前一樣顯示相同的行爲(微調不旋轉)。
Thread doBind = new Thread(new Runnable(){
public void run(){
doBindService();
}
});
doBind.start();
。
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
。
private boolean doBindService() {
mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((myService.LocalBinder)service).getService();
while (mBoundService.isRunning()){
}
//...do stuff same as before...
handler.sendEmptyMessage(0);
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
boolean myReturn = bindService(new Intent(myActivity.this, myService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = myReturn;
return myReturn;
}
我在進度對話框中看到了同樣的凍結行爲。這通常是在任務即將完成並且對話即將結束時。你的對話會持續多久?如果你讓線程睡幾秒鐘,是否會讓對話框旋轉? – softarn 2010-11-17 12:31:29
我有一個ProgressDialog類似的問題,我在這裏問:http://stackoverflow.com/questions/3821306/progressdialog-created-from-oncreatedialog-stops-animating-on-second-run。我認爲Android代碼庫中存在一個錯誤。 – ageektrapped 2010-11-17 12:40:22
@softarn: 感謝您的建議。如果我做的: 線程doBind =新主題(新的Runnable(){ \t公共無效的run(){ \t嘗試{ \t \t \t了Thread.sleep(10000); \t \t}趕上(InterruptedException的E){ \t \t \t e.printStackTrace(); \t \t} doBindService(); \t} }); 該對話框旋轉10秒,然後凍結3至4秒,然後消失... – John 2010-11-17 12:54:49