发送数据时使用和显示进度对话框

时间:2018-11-29 18:04:04

标签: android progressdialog

我是android上的新手,我希望在用户每次点击sndbtn并将数据发送到我的API时以及当应用程序完成数据发送时都显示进度条。

我不确定如何使用已有的代码来实现进度。请帮忙吗?

sndbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (networkInfo != null && networkInfo.isConnected()) {

                    Request data = helper.sndData(Integer.parseInt(id));
                    request = new Request(Activity.this, API.POST, data);
                    try {
                        String response = request.execute("url").get();

                        Response response = new Response(response);
                        if (responseListModel.isSuccess()) {

                            Toast.makeText(getApplication(), responseListModel.getMessage(), Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(Activity.this, NewActivity.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(getApplication(), responseListModel.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }

            } else {
                Toast.makeText(getApplication(), "Internet Error.", Toast.LENGTH_SHORT).show();
            }

        }
    });

2 个答案:

答案 0 :(得分:0)

使用此代码:

public ProgressDialog dialog;

        public void showDialog() {
            if (dialog == null) {
              dialog = new ProgressDialog(getContext());
            }
            dialog.setMessage("Searching");
            dialog.show();

        }

        public void hideDialog(){
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
        }

在您的代码中使用以下方法:

sndbtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        if (networkInfo != null && networkInfo.isConnected()) {

            showDialog();
            Request data = helper.sndData(Integer.parseInt(id));
            request = new Request(Activity.this, API.POST, data);
            try {
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        String response = request.execute("url").get();

                        Activity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                hideDialog();
                                Response response = new Response(response);
                                if (responseListModel.isSuccess()) {

                                    Toast.makeText(getApplication(), responseListModel.getMessage(), Toast.LENGTH_SHORT).show();

                                    Intent intent = new Intent(Activity.this, NewActivity.class);
                                    startActivity(intent);
                                } else {
                                    Toast.makeText(getApplication(), responseListModel.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }).start();

            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

        } else {
            Toast.makeText(getApplication(), "Internet Error.", Toast.LENGTH_SHORT).show();
        }

    }
});

答案 1 :(得分:0)

我建议使用AsyncTask并在onPreExecute中显示对话框,并在onPostExecute中关闭对话框。

/**
 * Check holiday schedule
 */
class CheckHolidayNoteAsync(context: Activity) : AsyncTask<Void, Void, HolidayScheduleInfo>() {

    override fun doInBackground(vararg params: Void?): HolidayScheduleInfo {
        //do network stuff here
        return HolidayScheduleInfo(result)
    }

    override fun onPostExecute(result: HolidayScheduleInfo) {
        //close dialog
    }



    /**
     * Runs on the UI thread before [.doInBackground].
     *
     * @see .onPostExecute
     *
     * @see .doInBackground
     */
    override fun onPreExecute() {
       //show progress dialog
    }
}