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);
        }
    };

これは私がどこかから取った上記のコードです...このコードが言うように、プログレスバーを実行し続けるために、実際のコードをtryの下に保持する必要があります..私のコードは、ネストされたforループで構成される非常に長いコードです(これらすべてのビットとバイトを含むファイル全体を解析する) ..セクションのその部分で長いプロセスを維持する場合、タスクが完了するまで進行状況バーを更新するにはどうすればよいですか? いくつかの概念がありませんか?プロセスを実行し続け、進行状況バーを更新する方法を教えてください。

4

2 に答える 2

1

onPreExecute()AsynTask を使用して、そのメソッドでProgreesBar を表示し、そのメソッドで ProgressBar を閉じる必要がありますonPostExecute()。すべての Loading Stuff をdoInBackground()method.Further で実行します。さらに、ProgressBar の使用を更新するにはonProgressUpdate()

これは役立ちます:http://developer.android.com/reference/android/os/AsyncTask.html

于 2012-04-29T12:28:49.943 に答える
0

あなたが見逃している概念は、複数のことをしたい場合(プログレスバーに進行状況を表示し、実際の作業を行う)、複数のスレッドを使用する必要があるということだと思います。そのため、実際の作業を行うために別のスレッドを作成する必要があります。

コードが非常に長い場合でも、コードをリファクタリングして、新しいクラスとメソッドに分割してみてください。

于 2012-04-29T12:35:34.170 に答える