【问题标题】:prograss bar not displaying using AsyncTask in android在android中使用AsyncTask不显示进度条
【发布时间】:2015-05-01 09:59:43
【问题描述】:

我刚刚写了一个代码,需要在与服务器交互时显示进度条。但是如果我在下面的代码中调用解除方法,它就不起作用了。

public class AsyncClass extends AsyncTask<String, String, String> {
    String result;
    public ProgressDialog progressbar;
    static Context context; 


    public AsyncClass(Context context,String result) {
        this.context = context;
        this.result = result;
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            URI u = new URI(params[0]);
            // url = Urls.SENDMOVIE_REQUEST_URL;
            HttpGet HG = new HttpGet(u);
            HttpResponse response = httpclient.execute(HG);
            //httpEntity = httpResponse.getEntity();
            result = EntityUtils.toString(response.getEntity());
            Log.d("result", result);
        } catch(Exception e) {
            e.printStackTrace();
        }

        return result;  
    }

    @Override
    protected void onPreExecute() {
        progressbar = new ProgressDialog(context);
        progressbar.setCancelable(true);
        progressbar.setMessage("loading....");
        progressbar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressbar.setProgress(0);

        progressbar.show();
    }

    @Override
    protected void onPostExecute(String result) {
        progressbar.dismiss(); 
    }
}

【问题讨论】:

  • 检查进度条的声明。不需要全局初始化对象吗?
  • 你试过在onProgressUpdate()里面做吗?
  • @Iqbal 在他的异步任务中是全局的
  • 如何启动 AsyncTask?
  • 尝试删除这一行:progressbar.setProgress(0);

标签: android android-asynctask android-progressbar


【解决方案1】:

我认为它没有显示是因为progressbar.setProgress(0);

只需删除它并尝试。

如果你想要progressUpdates,你需要这样做:

private class DownloadTask extends AsyncTask<String, String, String> {

    @Override 
    protected void onPreExecute() { 
        super.onPreExecute(); 
    } 

    @Override 
    protected String doInBackground(String... urls) {
        //Copy you logic to calculate progress and call 
        publishProgress("" + progress); 
    } 

    protected void onProgressUpdate(String... progress) {        
    mProgressDialog.setProgress(Integer.parseInt(progress[0]));
    } 

    @Override 
    protected void onPostExecute(String result) {           
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
    } 
} 

编辑伊克巴尔的回答

 private class ProgressTask extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog dialog;

        public ProgressTask(){ 
           dialog = new ProgressDialog(HomeActivity.this);
           dialog.setCancelable(false);
        } 

        /** progress dialog to show user that the backup is processing. */  
        /** application context. */ 
          @Override 
         protected void onPreExecute() {  
               this.dialog.setMessage("Please wait"); 
                this.dialog.show(); 
          } 

          protected Boolean doInBackground(final String... args) {
                  /** * Write your code */  
           } 

          @Override 
           protected void onPostExecute(final Boolean success) { 
                if (dialog.isShowing()) { 
                      dialog.dismiss(); 
                }  
                if (success) { 
                    // display UI 
                 } 
           } 
     } 

【讨论】:

    【解决方案2】:

    使用下面的代码它会为你完成这项工作

    private class ProgressTask extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog dialog = new ProgressDialog(HomeActivity.this);
        /** progress dialog to show user that the backup is processing. */ 
        /** application context. */
          @Override
         protected void onPreExecute() { 
               this.dialog.setMessage("Please wait"); 
                this.dialog.show(); 
          }
    
          protected Boolean doInBackground(final String... args) {
                  /** * Write your code */ 
           }
    
          @Override
           protected void onPostExecute(final Boolean success) { 
                if (dialog.isShowing()) { 
                      dialog.dismiss(); 
                } 
                if (success) { 
                    // display UI
                 }
           }
     }
    

    编辑:

    在构造函数中添加进度条的初始化为

    public AsyncClass(Context context,String result) {
            this.context = context;
            this.result = result;
            progressbar = new ProgressDialog(context);
    }
    

    并将其从 internecine() 中删除。

    【讨论】:

    • 然后在 asyncTask 的构造函数中声明进度对话框,并将您的活动的上下文传递给 asyncTask。
    • 那么问题可能出在调用 asyncTask 上。你是怎么做到的?你调试了吗?执行流程如何。合适吗?
    • 否。如果我在 post 中不使用关闭,则执行它可以正常工作,但一段时间后不会关闭
    • 好的..检查我的答案中的编辑,希望这会对你有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 2013-09-16
    • 2014-10-23
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多