简体   繁体   English

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

[英]Run a progress dialog while an other process is running

I am trying to run a progress dialog while an other process is running, and I can't do it. 我正在尝试在其他进程正在运行时运行进度对话框,但我做不到。

the process that I want run is this: 我要运行的过程是这样的:

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();
    }
}

this is launching when I click a button. 当我单击一个按钮时,这将启动。 thanks! 谢谢!

EDIT///////////// 编辑/////////////

well I solved the problem with this code!! 好吧,我用这段代码解决了问题!

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;
    }

  }

but I don't use a toast in a method doInBackground (FC) 但我没有在doInBackground(FC)方法中使用烤面包

Do you mean you are waiting for hilo to finish in this line : while(hilo.isAlive()){} ? 您是说您正在等待hilo在这一行完成吗: while(hilo.isAlive()){}

Use AsyncTask instead, make use of its onPostExecute 改用AsyncTask ,利用其onPostExecute

If your purpose is to show a ProgressDialog, then you can use this function for displaying a progress dialog- 如果您的目的是显示一个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);
}

In this finction, pass the context and duration in milliseconds for which you want to show, after that duration it will get dissmissed by itself. 在这一部分中,传递要显示的上下文和持续时间(以毫秒为单位),在该持续时间之后,它会自行消失。

you can try like this your process will run in background and progress dialog will be executing as well but when process will complete progress dialog will dismiss.. 您可以尝试这样,您的流程将在后台运行,进度对话框也将执行,但是当流程完成时,进度对话框将关闭。

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


}

U can call it onclick or also can onCreate .. it better to make separate class for asyntask1 and write new asynTask1(MainActivity.this).execute(); U可以将其称为onclickonCreate ..最好为asyntask1创建单独的类并编写new asynTask1(MainActivity.this).execute(); where you want to call it.. 您想在哪里打电话。

i hope it will helps ... 我希望这会有所帮助...

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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