Tung Vo
Tung Vo

Reputation: 2549

Stop indeterminate progress bar android

My android application use ProgressDialog. When send http request to server (RESTFul Webservice) the dialog is showed, when receive result from server the dialog is hided

private class GetMyTimeSheetTask extends AsyncTask<String, Void, String>
{
    private ProgressDialog progress;

    protected String doInBackground(String... params) {

        MyTimeSheetFragment.this.getActivity().runOnUiThread(new Runnable() {
            public void run() {
                progress = new ProgressDialog(MyTimeSheetFragment.this.getContext());
                progress.setTitle(getResources().getString(R.string.app_name));
                progress.setMessage(getResources().getString(R.string.loading));
                progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                progress.setCancelable(false);
                progress.show();
            }
        } );

        String result = HttpUtil.httpGet(
                params[0], params[1],
                params[2], params[3],
                params[4]);
        return result;
    }

    protected void onPostExecute(String param) {
        progress.dismiss();
        ....
        }
    }

The problem is sometime server has problem, the progress dialog is showed forever, i do not know how to close it. How to solve this problem? Thank for any help.

Upvotes: 1

Views: 854

Answers (2)

罗志辉
罗志辉

Reputation: 21

Your HttpUtil should return an error message when error occur, and timeout information when time out, so you can hide the dialog when you receive these exception messages. I can‘t see HttpUtil code, here maybe a solution so far:

private class GetMyTimeSheetTask extends AsyncTask<String, Void, String> {  
    private static final int REQUEST_TIMEOUT = 5000;
    private ProgressDialog progress;
    private Handler handler = new Handler(Looper.getMainLooper());
    private Runnable hideDialogTask = new Runnable(){
        @Override
        public void run() {
            progress.dismiss();
        }
   };

    protected String doInBackground(String... params) {

        MyTimeSheetFragment.this.getActivity().runOnUiThread(new Runnable(){
            public void run() {
            progress = new ProgressDialog(MyTimeSheetFragment.this.getContext());
            progress.setTitle(getResources().getString(R.string.app_name));
            progress.setMessage(getResources().getString(R.string.loading));
            progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progress.setCancelable(false);
            progress.show();
            handler.postDelayed(hideDialogTask, REQUEST_TIMEOUT);
        }
    } );

    String result = HttpUtil.httpGet(
            params[0], params[1],
            params[2], params[3],
            params[4]);
    return result;
}

protected void onPostExecute(String param) {
    progress.dismiss();
    ....
    }
}

Upvotes: 2

Pooja Rajendran C
Pooja Rajendran C

Reputation: 452

Try implementing a timeout scenario along with async task

Upvotes: 1

Related Questions