如何在Android应用程序中创建进度对话框?

6
我正在开发一个从互联网接收数据的应用程序,在接收数据时我想显示“进度对话框”。我在我的应用程序中使用了“AsyncTask”。 问题是如何使用它,并如何显示百分比,例如100%? 请给我建议并且提供一些例子。 谢谢,对我的英语表示抱歉。

请查看文档,其中包含一个示例:http://developer.android.com/reference/android/os/AsyncTask.html。 - Raghunandan
由于ProgressDialog已被弃用,可能有什么替代方案。 - jeevan s
2个回答

28
为显示进度对话框,您可以使用以下代码。
 ProgressDialog dialog = new ProgressDialog(MainActivity.this);
                dialog.setMessage("Your message..");
                dialog.show();

在调用异步任务(即在new YourTask.execute().之前),您可以进行以下操作:

在asynctask的onPostExecute函数中,您可以进行以下操作:

 dialog.dismiss();

关闭对话框。


1
显示进度百分比。您可以查看此示例... [链接](http://www.androidhive.info/2012/04/android-downloading-file-by-showing-progress-bar/) - 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();
}

希望这对你们都有效。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接