java - ProgressDialog 出现得太晚

标签 java android eclipse

以下是我的代码:

    public void onClick(View view) {


    dialog=new ProgressDialog(view.getContext());
    dialog.setCancelable(true);
    dialog.setMessage("Initializing Please Wait");
    dialog.setTitle("Loading");

    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    dialog.setProgress(0);
    dialog.setMax(100);
    dialog.show();
    progressStatus=0;
    new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                while(progressStatus<10)
                {
                    progressStatus=performTask();


                }
                /*Dismiss the Progress bar*/
                myHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        dialog.dismiss();//dismiss the dialog

                       Toast.makeText(getBaseContext(),msg,Toast.LENGTH_SHORT).show();
                       progressStatus=0;

                       myProgress=0;

                    }
                });

            }
            /* Do some task*/
            private int performTask()
            {
                try {
                    //---simulate doing some work---
                    Thread.sleep(1000);
                    } catch (InterruptedException e)
                    {
                    e.printStackTrace();
                    }
                    return ++myProgress;    
            }
        }).start();
}

每当我单击按钮时,大约 5 秒后就会出现进度对话框。我希望我的进度对话框在我单击按钮后立即显示。紧急情况请有人帮忙吗?提前致谢。

最佳答案

请执行以下操作 -

class RemoveAssignedVehicle extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Initializing Please Wait");
            pDialog.setTitle("Loading");
            pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            progressStatus=0;
            pDialog.show();
        }


        protected String doInBackground(String... args) {
             // TODO Auto-generated method stub
            while(progressStatus<10)
            {
                progressStatus=performTask();
            }

            return "";
        }

        protected void onPostExecute(String result) {

            pDialog.dismiss();

            Toast.makeText(getBaseContext(),msg,
                    Toast.LENGTH_SHORT).show();
            progressStatus=0;

            myProgress=0;
        }

关于java - ProgressDialog 出现得太晚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24445119/

相关文章:

java - Java 中枚举的命名 : Singular or Plural?

android - 如何设置语音识别延迟?

python - Eclipse + Pydev + twitter 库问题

尝试从 jar 实例化类时出现 java.lang.NoClassDefFoundError

java - 什么是更好的浅克隆或深度克隆?

java - 是否可以通过 JAXB 2.2.x 解码非 XML 数据?

java - Firebase 存储图像检索 RecyclerView

c++ - QtCreator——显示一个类的成员函数列表?

java - 替换扫描仪字符串中的字符

android - 覆盖 startResolutionForResult 的对话框