简体   繁体   English

进度条在 AsyncTask 中不起作用

[英]Progressbar not working inside AsyncTask

I am using an AsyncTask to download some big amount of data from a server my AsyncTask work fine so i added a progress bar to make everything beautiful but problem is when its running progress bar get freeze half way down, and i use progress dialog that also the same its freeze half way down,我正在使用AsyncTask从服务器下载一些大量数据,我的 AsyncTask 工作正常,所以我添加了一个进度条,使一切变得漂亮,但问题是当它的运行进度条在中途冻结时,我使用进度对话框同样它冻结到一半,

private class downloadChannelsfromserver extends
        AsyncTask<String, Void, String> {

     ProgressDialog progressDialog;

    @Override
    protected String doInBackground(String... url) {
        String data = "";

        try {
            // Fetching the data from web service
            data = getLinksfromServer(url[0]);
        } catch (Exception e) {
            Log.d("Background Task", e.toString());
        }
        return data;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
         progressDialog= ProgressDialog.show(Settings.this, "Synchronicing","Synchronicing", true);


    }



    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        JSONObject json;
        try {
            json = new JSONObject(result);
            db.deleteAll();
            final JSONArray jsonArray = json.getJSONArray("XXXX");

            for (int i = 0; i < jsonArray.length(); i++) {
                                ///use for insert datainto database
            }
            finish();

            progressDialog.dismiss();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            Log.e("settings", "", e);
            progressDialog.dismiss();

        }
    }

can and someone tell me why this happen, Pls可以有人告诉我为什么会这样,请

Follow this code按照这个代码

private ProgressDialog pdialog = null;

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

   if(pdialog ==null){
     //display progress dialog like this
     pdialog = new ProgressDialog(context);
     pdialog.setCancelable(false);
     pdialog.setMessage("Please Wait...");
     pdialog.show();
   }
}



@Override
   protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        //dismiss progress dialog like this

        if(pdialog!=null){
             pdialog.dismiss();
             pdialog = null;
        }
    }

Reason for progress bar get stuck was my JSON decoding since onPostExecute belongs to UI thread it take several time to decode the json results.进度条卡住的原因是我的JSON解码,因为onPostExecute属于 UI 线程,解码 json 结果需要一些时间。 so it will freeze the UI until JSON decode so move the decoding part to doInBackground will solve the UI freeze issue since doInBackground belongs to background thread所以它会冻结 UI 直到 JSON 解码所以将解码部分移动到doInBackground将解决 UI 冻结问题,因为doInBackground属于后台线程

Please edit your code like this:请像这样编辑您的代码:

private class downloadChannelsfromserver extends
    AsyncTask<String, Void, String> {

 ProgressDialog progressDialog;

public downloadChannelsfromserver()
{
    progressDialog = new ProgressDialog(Settings.this);
    progressDialog.setTitle("Synchronicing ...");
    progressDialog.setCancelable(false);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
}

@Override
protected String doInBackground(String... url) {
    String data = "";

    try {
        // Fetching the data from web service
        data = getLinksfromServer(url[0]);
    } catch (Exception e) {
        Log.d("Background Task", e.toString());
    }
    return data;
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    //super.onPreExecute();
  progressDialog.show();

}



@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    JSONObject json;
    try {
        json = new JSONObject(result);
        db.deleteAll();
        final JSONArray jsonArray = json.getJSONArray("XXXX");

        for (int i = 0; i < jsonArray.length(); i++) {
                            ///use for insert datainto database
        }
        finish();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        Log.e("settings", "", e);
    }
    finally
    {
     progressDialog.dismiss();
    }
}

I hope this helps.我希望这有帮助。

Just Use This Code:只需使用此代码:

runOnUiThread(object : Runnable {
    override fun run() {
        pausingDialog =
            SweetAlertDialog(this@History_Activity, SweetAlertDialog.PROGRESS_TYPE)
        pausingDialog!!.titleText = "Please wait...."
        pausingDialog!!.setCancelable(false)
        pausingDialog!!.show()
    }
})

runOnUiThread(object : Runnable {
    override fun run() {
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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