在其他进程运行时运行进度对话框

时间:2014-11-19 09:46:21

标签: android progressdialog

我正在尝试在其他进程运行时运行进度对话框,但我无法执行此操作。

我想要运行的过程是这样的:

public void Onclick_editar (View view)
{
    float porc;
    String repre;
    try{
        porc = Float.parseFloat(edit_porcentaje.getText().toString());
        repre = edit_representante.getText().toString();
        try
        {
            Hilo hilo;
            hilo = new Hilo(2, "UPDATE eleccion SET porcentaje="+porc+", delegados='"+repre+"' WHERE id="+idd );
            hilo.start();
            while(hilo.isAlive()){}

            hilo = new Hilo(4, idd);
            hilo.start();
            while(hilo.isAlive()){}
            Toast.makeText(this, "¡Actualizada con éxito!", Toast.LENGTH_SHORT).show();

        }
        catch(SQLException e)
        {
            Toast.makeText(this, "Error: Fallo en la base de datos", Toast.LENGTH_SHORT).show();
        }                   
    }catch(Exception e)
    {
        Toast.makeText(this, "Rellena todos los campos", Toast.LENGTH_SHORT).show();
    }
}

这是我点击按钮时启动的。谢谢!

EDIT /////////////

我用这段代码解决了问题!!

public void button(View v) {

     new waiting().execute();

}

  class waiting extends AsyncTask<String, Void, Boolean> {

      ProgressDialog progressdialog;
      protected void onPostExecute(Boolean result) {
             super.onPostExecute(result);
             progressdialog.dismiss();

         }

      protected void onPreExecute() {
          super.onPreExecute();
          // Shows Progress Bar Dialog and then call doInBackground method
          progressdialog = new ProgressDialog(Tab_adminver_modificar_eleccion.this);
          progressdialog.setTitle("Processing....");
          progressdialog.setMessage("Please Wait.....");
          progressdialog.setCancelable(false);
          progressdialog.show();         
      }
    @Override
    protected Boolean doInBackground(String... params) {
        try {
                                Thread.sleep(5000);

                            } catch (Exception e) {

                            }
        return null;
    }

  }

但我没有在doInBackground(FC)方法中使用吐司

4 个答案:

答案 0 :(得分:1)

您的意思是等待hilo完成此行:while(hilo.isAlive()){}

请改用AsyncTask,使用其onPostExecute

答案 1 :(得分:0)

如果您的目的是显示ProgressDialog,那么您可以使用此功能显示进度对话框 -

public static void showProgressLoader(Context context, int duration)
{
    final ProgressDialog dialog = ProgressDialog.show(context, "", "Loading...");
    dialog.show();

    Handler handler = new Handler();
    handler.postDelayed(new Runnable()
    {
        public void run()
        {
            dialog.dismiss();
        }
    }, duration);
}

在此结尾处,传递您想要显示的上下文和持续时间(以毫秒为单位),在此持续时间之后,它将被自己解除。

答案 2 :(得分:0)

你可以尝试这样你的过程将在后台运行,进度对话框也会执行,但是当进程完成进度时,对话框将会解除..

public class asynTask1 extends AsyncTask<String, Void, Boolean> {

    public asynTask1 (MainActivity activiy) {
        context = activiy;
    }

    @Override
    protected void onPostExecute(Boolean result) {

        super.onPostExecute(result);

        progressdialog.dismiss(); 




    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
        progressdialog = new ProgressDialog(Splash.this);
        progressdialog.setTitle("Processing....");
        progressdialog.setMessage("Please Wait.....");
        progressdialog.setCancelable(false);
        progressdialog.show();

    }

    @Override
    protected Boolean doInBackground(String... params) {

    // do process


}

你可以调用它onclick或者也可以调用onCreate ..最好为asyntask1制作单独的类并写 new asynTask1(MainActivity.this).execute();您要将其称为..

我希望它会有所帮助...

答案 3 :(得分:0)

public void launchRingDialog(View view) {

    final ProgressDialog ringProgressDialog = ProgressDialog.show(MainActivity.this, "Please wait ...", "Downloading Image ...", true);

    ringProgressDialog.setCancelable(true);

    new Thread(new Runnable() {

        @Override

        public void run() {

            try {

                **// the MYSql consultation should be here?**

            } catch (Exception e) {



            }

            ringProgressDialog.dismiss();

        }

    }).start();