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

希望它为你所有。

相关问题