2010-11-17 58 views
5

這是我的拳頭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; 
} 
+0

我在進度對話框中看到了同樣的凍結行爲。這通常是在任務即將完成並且對話即將結束時。你的對話會持續多久?如果你讓線程睡幾秒鐘,是否會讓對話框旋轉? – softarn 2010-11-17 12:31:29

+0

我有一個ProgressDialog類似的問題,我在這裏問:http://stackoverflow.com/questions/3821306/progressdialog-created-from-oncreatedialog-stops-animating-on-second-run。我認爲Android代碼庫中存在一個錯誤。 – ageektrapped 2010-11-17 12:40:22

+0

@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

回答

2

與微調的問題是有關您在新線程創建ServiceConnection的處理函數做到這一點。不幸的是,這將在你的主線程/ gui線程上運行。

從API參考:(http://developer.android.com/reference/android/content/ServiceConnection.html)

喜歡從系統許多回調, 上此類的方法是從您的流程的主線程調用 。

因此,由於我對服務不熟悉,我只能猜測服務完成下載數據時進行通信的一種方式是廣播您在活動中偵聽的Intent,然後停止ProgressDialog。但也有其他方法。

作爲一個方面說明,你不應該從主線程以外的任何線程修改gui,你嘗試通過產生一個試圖關閉對話框的新線程來完成。你只應該關閉主線程中的對話框。

+0

我正在調查您的建議(發送並對意圖做出反應),目前看起來這是我的問題的答案。你在你的文章中說過「還有其他一些方法。」 ...這些其他方式是什麼?這些中的任何一個都可以成爲這樣做的「更好」方式? – John 2010-11-17 16:58:42

+0

使用你的建議,我發現這個職位:http://stackoverflow.com/questions/2843874/in-android-how-to-call-function-of-activity-from-a-service現在有一個工作的應用程序。謝謝你的幫助! – John 2010-11-17 17:33:13

0

乾草你可以嘗試使用處理程序嗎?

,我認爲你應該使用Android

+0

嗨。感謝您的建議。我嘗試過實現一個處理程序來做到這一點,但我不清楚它的調用應該在哪裏以及它應該實際做什麼。我當前的實現仍然顯示與使用線程時描述的相同的問題。我使用了http://www.helloandroid.com/tutorials/using-threads-and-progressdialog上看到的例子。我將編輯我的原始文章並添加我嘗試過的代碼。如果你可以看看,看看我做錯了,我會感激。 – John 2010-11-17 14:58:16

0
public class yourclass extends Activity implements Runnable { 

    static ProgressDialog progressDialog; 

    @Override 
    public void onCreate(Bundle icicle) { 
       progressDialog = ProgressDialog.show(keywordview.this, "", "Loading...", true); 
       Thread thread = new Thread(keywordview.this); 

    } 
    final Handler handler = new Handler() { 

     @Override 
     public void handleMessage(Message msg) { 
      progressDialog.dismiss(); 
     /*Here you can handle you process*/ 
     } 
    }; 

    public void run() { 
     //here your code for run 
     handler.sendEmptyMessage(0); 
    } 
}