简体   繁体   中英

Can't show a ProgressDialog from an AsynkTask

I'm trying to show a ProgressDialog from an AsyncTask but is not showing... if I delete the 'dialog.dismiss()' of the PostExecute, this task works like a 'post()', it shows the Dialog in the end of the task. I'm getting crazy with this!

private static class Experimento extends AsyncTask<String, Void, String> 
{   
    ProgressDialog dialog;

    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();

        dialog = new ProgressDialog(Config.getCurrent_Context());
        dialog.setMessage("Cargando...");
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
    }

    protected String doInBackground(String... params) 
    {   
        try
        {     
            Config.getCurrent_Context().runOnUiThread(new Runnable()
            {   
                @Override
                public void run()
                {
                    try
                    {
                        //Work, work, work...
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            });
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String result) 
    {
        super.onPostExecute(result);

        dialog.dismiss();
    }
}

Now tried giving the Activity in the constructor but still not working...

private static ProgressDialog dialog;

    public Experimento(Activity act)
    {
        Log.w("Experimento", "Experimento");
        dialog = new ProgressDialog(act);
        dialog.setMessage("Cargando...");
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
    }

您应该删除setIntermidiate(true);。

Try this, it works for me:

  Override
    protected void onPreExecute() 
    {

        dialog = new ProgressDialog(Config.getCurrent_Context());
        dialog.setMessage("Cargando...");
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
    }

    protected String doInBackground(String... params) 
    {   
  String result = " ";
        try
        {     
            Config.getCurrent_Context().runOnUiThread(new Runnable()
            {   
                @Override
                public void run()
                {
                    try
                    {
                        //Work, work, work...
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            });
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }

    protected void onPostExecute(String result) 
    {

        dialog.dismiss();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM