Android的进度对话框 [英] Android progress dialog

查看:119
本文介绍了Android的进度对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序从互联网上获取一些HTML code和完成后,在设备屏幕上显示出来。由于需要约3-4秒,要做到这一点,就在这个时候,屏幕一直是黑色的,我想用一个进度对话框。这是我的code:

My application fetches some html code from the internet and when done , displays it on the devices screen. Since it takes about 3-4 seconds to do that , in this time the screen stays black , I'd like to use a progress dialog. This is my code :

package com.nextlogic.golfnews;



// ALL THE IMPORTS ....

public class Activity1 extends Activity {

    private ProgressDialog progressDialog;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        progressDialog = ProgressDialog.show(Activity1.this, "", "Loading...");
        new Thread() 
        {
          public void run() 
          {

             try
               {
                sleep(2000);

          // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME
 }
    catch (Exception e)
    {
        Log.e("tag",e.getMessage());
    }
// dismiss the progressdialog   
  progressDialog.dismiss();
 }
}.start();

该项目工程,但它已经不显示任何内容。我在logcat中有一个错误:

The program works but it doesn't display anything anymore. I have one error in logcat :

Only the original thread that created a view hierarchy can touch its views.

你能帮帮我吗?先谢谢了。

Could you please help me ? Thanks in advance.

推荐答案

误差足够explicative。要更新一个视觉对象,你必须运行主线程内的变化。一个快速和肮脏的修补程序将调用内部 runOnUiThread()更新code。

The error is explicative enough. To update one visual object you must run the changes inside main thread. A quick and dirty fix could be calling the update code inside runOnUiThread().

不过你的情况我会用一个的AsyncTask 的下载和更新的进度进度条。任务有当它结束UI线程上运行的财产(这样你就可以有更新的意见,如驳回进度对话框)

However in your case I would use an AsyncTask to download and update the progress of the progress bar. The task has the property to run on UI thread when it ends (so you can update the views there, such as dismissing the progress dialog)

<一个href="http://stackoverflow.com/questions/6053836/how-to-use-asynctask-class-to-update-the-progress-of-copying-a-file-another-direc/6053982#6053982">Here是一个例子,如何使用的AsyncTask显示下载进度对话框。

Here is an example how to use an AsyncTask to display a download progress dialog.

更新

#1已经拥有了你所有的问题。 <一href="http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog/3028660#3028660">Here是一种的AsyncTask的一例下载一些内容,并显示下载进度。只要你想要的东西。

Stackoverflow already has the answers to all your question. Here is an example of an AsyncTask to download some content and display the download progress. Just what you want.

更新2

确定这里是一个使用的AsyncTask的code:

Ok here is your code using an AsyncTask:

public class Activity1 extends Activity
{
    private ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        new AsyncTask<Integer, Integer, Boolean>()
        {
            ProgressDialog progressDialog;

            @Override
            protected void onPreExecute()
            {
                /*
                 * This is executed on UI thread before doInBackground(). It is
                 * the perfect place to show the progress dialog.
                 */
                progressDialog = ProgressDialog.show(Activity1.this, "",
                        "Loading...");
            }

            @Override
            protected Boolean doInBackground(Integer... params)
            {
                if (params == null)
                {
                    return false;
                }
                try
                {
                    /*
                     * This is run on a background thread, so we can sleep here
                     * or do whatever we want without blocking UI thread. A more
                     * advanced use would download chunks of fixed size and call
                     * publishProgress();
                     */
                    Thread.sleep(params[0]);
                    // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME
                }
                catch (Exception e)
                {
                    Log.e("tag", e.getMessage());
                    /*
                     * The task failed
                     */
                    return false;
                }

                /*
                 * The task succeeded
                 */
                return true;
            }

            @Override
            protected void onPostExecute(Boolean result)
            {
                progressDialog.dismiss();
                /*
                 * Update here your view objects with content from download. It
                 * is save to dismiss dialogs, update views, etc., since we are
                 * working on UI thread.
                 */
                AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this);
                b.setTitle(android.R.string.dialog_alert_title);
                if (result)
                {
                    b.setMessage("Download succeeded");
                }
                else
                {
                    b.setMessage("Download failed");
                }
                b.setPositiveButton(getString(android.R.string.ok),
                        new DialogInterface.OnClickListener()
                        {

                            @Override
                            public void onClick(DialogInterface dlg, int arg1)
                            {
                                dlg.dismiss();
                            }
                        });
                b.create().show();
            }
        }.execute(2000);

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

                // dismiss the progressdialog
                progressDialog.dismiss();
            }
        }.start();
    }
}

这篇关于Android的进度对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆