我正在使用以下功能在我的 android 视图中创建/显示加载
public void showDialogue(String Message, String Title){
builder = new AlertDialog.Builder(this);
progress = new ProgressBar(this);
builder.setMessage(Message);
builder.setView(progress);
builder.create().show();
}
我将此函数称为异步任务
private class SetParm extends AsyncTask<String, Integer, String> {
Integer myid;
Integer myFlag;
Integer removeId;
@Override
protected String doInBackground(String... sUrl) {
try {
SharedPreferences mPrefs = getSharedPreferences("prefs",0);
String restoredText = mPrefs.getString("access_token", "");
String path = "http://www.sitename.com/app/setFlag.php";
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
HttpResponse response;
JSONObject json = new JSONObject();
try {
HttpPost post = new HttpPost(path);
json.put("access_token", restoredText);
json.put("id", myid);
json.put("flag", myFlag);
Log.i("jason Object", json.toString());
post.setHeader("json", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
/* Checking response */
if (response != null) {
InputStream in = response.getEntity().getContent();
String a = convertStreamToString(in);
JSONObject jsono = stringToJsonobj(a);
Log.v("TAGG",a);
String passedStringValue = jsono.getString("result");
if(passedStringValue.equals("1")){
flags=1;
//Log.v("TAGG", "Success");
SharedPreferences mPrefss = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = mPrefss.edit();
editor.putString("access_token", jsono.getString("access_token"));
editor.commit();
}
else {
flags=0;
//Log.v("TAGG", "Failed !");
}
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
}
return null;
}
@Override
protected void onPreExecute() {
showDialogue("Regestring Your Devide... Please wait.", "Regestring Devide");
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
}
@Override
protected void onPostExecute(String result) {
builder.cancel();
if(flags.equals(1)){
TableLayout mTable = (TableLayout)findViewById(R.id.tableLayout_1);
mTable.removeView(findViewById(4500+removeId));
mTable.removeView(findViewById(6500+removeId));
int count = mTable.getChildCount();
if(count<=0){
lists="";
total="0";
LogIN loginUsers1 = new LogIN();
loginUsers1.execute("");
}
}
else {
TextView text = (TextView) findViewById(R.id.status_msg);
text.setText("Error while processing requests. Please try again.");
}
super.onPostExecute(result);
}
}
从 onPreExecute() 函数调用警报对话框。
现在我需要在 web 服务请求完成后删除加载
所以我写builder.cancel();
了onPostExecute
但它不起作用
任何想法 ?提前致谢