How to execute an instruction/method immediately after ProgressDialog dismisses?

梦想的初衷 提交于 2019-12-24 05:58:16

问题


I am trying to execute the method doSomeWork(); after the ProgressDialog dismisses in my method printing();which seems to be overlapped by the other method and the dialog is not showed up. If I comment method doSomeWork(); the dialog is displayed correctly until the thread is finished.

Here is my method printing();

  public void printing()
{

    final ProgressDialog printingDialog = ProgressDialog.show(this, "Printing...", "Please wait", true, false);

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

          //something big executing here
        }
        }).start();

}

He is my method doSomework():

    public void doSomeWork(){

        Thread receiptPrint = new Thread(new Runnable() {

            @Override
            public void run() {

               //something here

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //another dialog here
                    }
                });

            }
            });
}

Here you can see the how I am calling those two methods:

    private OnClickListener onClickPrint = new OnClickListener() {

    public void onClick(final View v) {

            Log.d("Button","Clicked on Print Order Button");

                    printing();
                    doSomeWork();

Does anyone know how could I execute doSomeWork() only when printing(); will be completely finished?


回答1:


This is one of the purposes of an AsyncTask. It would look similar to this:

public void onClick(final View v) {

    new AsyncTask<Void, Void, Void>() {

        @Override
        protected void onPreExecute() {

            //Show your progress dialog in here
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground( Void... params ) {

            printing();
            return null;
        }

        @Override
        protected void onPostExecute( Void result ) {

            //Dismiss your progress dialog here
            doSomeWork();
        }
    }.execute();
}



回答2:


Instead of using thread you can use asynchronous task. Show the progress dialog in the preexecute method call the printing method inside the background method after completing printing operation call the doSomeWork() inside the postexecute method.




回答3:


You can use Handler for that in android. for example consider the following piece of code. you can dismiss the dialogs inside handlers. may it work for you.

private void printing(){
  Thread receiptPrint = new Thread(new Runnable() {

        @Override
        public void run() {
            retrieveEmails();

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                  try{
                      //here your code executes
                      //after code executes do following:
                      uiHandler.sendEmptyMessage(0);
                  }catch(Exception ex){
                      errorHandler.sendEmptyMessage(0);
                  }
                }
            });

        }
  });
  receiptPrint.start();
}

final Handler uiHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
            //here execute doSomeWork()
    }
};

final Handler errorHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
            //do other stuff
    }
};


来源:https://stackoverflow.com/questions/15708479/how-to-execute-an-instruction-method-immediately-after-progressdialog-dismisses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!