4

我正在開發應用程序以接收我想要顯示「進度對話框」的數據時從互聯網上接收一些數據。我在我的應用程序中使用了「AsyncTask」如何在Android應用程序中創建進度對話框?

問題是如何使用它以及如何顯示100%的百分比?

請建議我,給我一些例子。 謝謝你和我的英語對不起。

+0

http://developer.android.com/reference/android/os/AsyncTask.html。檢查文檔有一個示例 – Raghunandan

回答

22

你以前new YourTask.execute().

,並在的AsyncTask的onPostExecute功能,即調用異步任務之前要告訴你可以用下面的代碼

ProgressDialog dialog = new ProgressDialog(MainActivity.this); 
       dialog.setMessage("Your message.."); 
       dialog.show(); 

的prgress對話框中您可以使用

dialog.dismiss(); 

關閉對話框。

+0

讓我知道它是否有幫助... – shreyas

+1

用於顯示進度百分比。您可以查看此示例... [link](http://www.androidhive.info/2012/04/android -downloading文件逐表示-進度條/) – shreyas

2

可以使用休耕方法:

public void launchBarDialog(View view) { 
    barProgressDialog = new ProgressDialog(MainActivity.this); 

    barProgressDialog.setTitle("Downloading Image ..."); 
    barProgressDialog.setMessage("Download in progress ..."); 
    barProgressDialog.setProgressStyle(barProgressDialog.STYLE_HORIZONTAL); 
    barProgressDialog.setProgress(0); 
    barProgressDialog.setMax(20);//In this part you can set the MAX value of data 
    barProgressDialog.show(); 

    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try { 

       // Here you should write your time consuming task... 
       while (barProgressDialog.getProgress() <= barProgressDialog.getMax()) { 

        Thread.sleep(2000); 

        updateBarHandler.post(new Runnable() { 

         public void run() { 

          barProgressDialog.incrementProgressBy(1);//At this, you can put how many data is downloading by a time 
                    //And with the porcentage it is in progress 
         } 

        }); 

        if (barProgressDialog.getProgress() == barProgressDialog.getMax()) { 

         barProgressDialog.dismiss(); 

        } 
       } 
      } catch (Exception e) { 
      } 
     } 
    }).start(); 
} 

希望它爲你所有。

相關問題