我正在使用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();
}
}
能和某人告诉我为什么会这样,Pls
答案 0 :(得分:2)
请遵循此代码
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;
}
}
答案 1 :(得分:2)
进度条被卡住的原因是我的JSON
解码,因为onPostExecute
属于UI线程需要几次解码json结果。因此它将冻结UI直到JSON解码,因此将解码部分移动到doInBackground
将解决UI冻结问题,因为doInBackground
属于后台线程
答案 2 :(得分:1)
请编辑您的代码:
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();
}
}
我希望这会有所帮助。
答案 3 :(得分:0)
只需使用此代码:
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() {
}
})