简体   繁体   中英

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.

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

尝试实现超时方案以及异步任务

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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