2012-04-29 3 views
0

アンドロイドのプログレスバーに関する疑問があります。これは私がから取った上記のコード..ですAndroidプログレスバーの作業について

dialog = new ProgressDialog(this); 
     dialog.setCancelable(true); 
     dialog.setMessage("Loading..."); 
     // set the progress to be horizontal 
     dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     // reset the bar to the default value of 0 
     dialog.setProgress(0); 

     // get the maximum value 
     EditText max = (EditText) findViewById(R.id.maximum); 
     // convert the text value to a integer 
     int maximum = Integer.parseInt(max.getText().toString()); 
     // set the maximum value 
     dialog.setMax(maximum); 
     // display the progressbar 
     dialog.show(); 

     // create a thread for updating the progress bar 
     Thread background = new Thread (new Runnable() { 
      public void run() { 
       try { 
        // enter the code to be run while displaying the progressbar. 
        // 
        // This example is just going to increment the progress bar: 
        // So keep running until the progress value reaches maximum value 
        while (dialog.getProgress()<= dialog.getMax()) { 
         // wait 500ms between each update 
         Thread.sleep(500); 

         // active the update handler 
         progressHandler.sendMessage(progressHandler.obtainMessage()); 
        } 
       } catch (java.lang.InterruptedException e) { 
        // if something fails do something smart 
       } 
      } 
     }); 

     // start the background thread 
     background.start(); 

    } 

    // handler for the background updating 
    Handler progressHandler = new Handler() { 
     public void handleMessage(Message msg) { 
      dialog.incrementProgressBy(increment); 
     } 
    }; 

を私はそれを実装していない、まだなく、期限の不足に私は私が良く起こるために何が起こっているかと、なぜ明確に実装したときに確認しますどこかに...このコードでは、私は実際のコードを以下のようにしてプログレスバーを実行し続けるようにしなければならないと言います。私の疑問は、私のコードは非常に長いもので、ネストされたforループおよびバイト)..私はセクションのその部分で私の長いプロセスを維持する場合、どのようにタスクを完了するまで、進行状況バーを更新できますか? 私はいくつかの概念を欠いていますか?どうすれば私のプロセスを実行し続けることができますか、プログレスバーを更新することができますか?

答えて

1

AsynTaskを使用して、onPreExecute()メソッドでProgreesBarを表示し、onPostExecute()メソッドでProgressBarを終了する必要があります。 doInBackground() method.Furtherにすべてのロードものを行う、これが役立ちますプログレスバーがonProgressUpdate()

を使用し更新するには:http://developer.android.com/reference/android/os/AsyncTask.html

0

私はあなたが不足しているコンセプトは、あなたが複数の物事が(に進捗状況を示すためにしたい場合ということだと思いますプログレスバーといくつかの実際の作業をしている)、複数のスレッドを使用する必要があります。そのため、実際の作業を行うために別のスレッドを作成する必要があります。

あなたのコードが本当に長さであっても、それを再度ファクタリングして、新しいクラスとメソッドに分割してみてください。

+0

そのため、そのセクションにループを入れ子にしておくと、プログレスバーが複数のスレッドの原因を更新し続けるのでしょうか? – Tani

+0

別のスレッドで作業をしているので、アニメーションが残っているはずです。しかし、ネストされたループのどこかで進捗ハンドラを介してメインスレッドに進捗状況を伝える必要があります。 – Heinrisch

関連する問題