进度条加载消失

时间:2017-11-01 08:14:53

标签: android multithreading android-asynctask progress-bar

如何进行进度条加载直到执行doBackground()中的所有方法。我使用了Thread.sleep,加载程序显示之后,暂停执行所有方法,然后返回到下一个活动

 public  class LoadDataForActivity extends AsyncTask<Void, Void, Void> {



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

        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.execute(new Runnable() {
            public void run() {
                // Do some long running operation on a worker thread
                try {

                    Thread.sleep(200); // no need for a loop
                    cities_from();
                    cities_to();
                    srs_bus();
                    etsPick();
                    etsPick1();
                    srs_drop1();
                    srs_drop2();
                    srs_key_value();
                    key_value();
                    srs_pick1();
                    srs_pick2();
                    key_value_pick();
                    vrl_borad_id_loc();
                    vrl_board_key_value();
                    vrl_drop_id_loc();
                    vrl_drop_key_value();
                    vrl_bus();
                    bitla_key_value_pick2();
                    ets_bus();
                    bitla_drop();
                    bitla_drop1();
                    bitla_drop2();
                    bitla_key_value();
                    bitla_key_value1();
                    bitla_pick1();
                    bitla_pick2();
                    bitla();
                    srs();
                    tabs();
                    cancel_policy();
                } catch (InterruptedException e) {
                    Log.e("LongOperation", "Interrupted", e);

                }
            }
        });

        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd=new ProgressDialog(BusOperators.this);
        pd.setTitle("Title");
        pd.setMessage("Please Wait.... ");
        pd.setCancelable(false);
        pd.show();
    }

    protected void onPostExecute(Void v) {
            super.onPostExecute(v);
            pd.dismiss();
    }
    protected void onProgressUpdate(String... progress) {
        // Update UI to show progress
    /* prgDialog.setProgress(Integer.parseInt(progress[0])); */
    }

}

我希望进度条保持不变,直到doBackground()中的所有方法都被执行

2 个答案:

答案 0 :(得分:1)

你必须设置:

progressBar.setVisibility(VISIBLE)
在开始运行线程之前

,并在onPostExecute set:

progressBar.setVisibility(GONE)

答案 1 :(得分:0)

使用executor.execute时,控件将直接返回,控件将转到方法onPostExecute

只需从doInBackground

中的executor.execute方法中解包您的代码