我无法停止进度对话框。BackgroundWorkPractivity用于用户授权和注册。当用户在登录布局上填写用户名和密码字段,并单击登录按钮时,系统将启动授权和进度对话框并开始循环,当出现进入下一布局的消息框时停止。另一方面,当用户使用错误的数据或字段为空时,此进度对话框不会消失。消息框显示“字段为空”或“登录凭据无效”,但进度对话框仍保留在布局上。以下是背景实用性:
public class BackgroundWorker extends AsyncTask<String, Void, String >{
Context context;
AlertDialog alertDialog ;
BackgroundWorker (Context ctx){
context = ctx;
}
ProgressDialog loading;
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://sultonkhuja1111.000webhostapp.com/MobApp/auth_login.php";
String register_url = "http://sultonkhuja1111.000webhostapp.com/MobApp/registration.php";
if(type.equals("login")){
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while ((line = bufferedReader.readLine())!= null){
result+=line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else if(type.equals("register")){
try {
// String spnUsertype = params[1];
String txtFirst = params[1];
String txtLast = params[2];
String txtEmail = params[3];
String txtUsername = params[4];
String txtPassword = params[5];
String txtPhone = params[6];
URL url = new URL(register_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = //URLEncoder.encode("user_type","UTF-8")+"="+URLEncoder.encode(spnUsertype,"UTF-8")+"&"
URLEncoder.encode("first","UTF-8")+"="+URLEncoder.encode(txtFirst,"UTF-8")+"&"
+ URLEncoder.encode("last","UTF-8")+"="+URLEncoder.encode(txtLast,"UTF-8")+"&"
+ URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(txtEmail,"UTF-8")+"&"
+ URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(txtUsername,"UTF-8")+"&"
+ URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(txtPassword,"UTF-8")+"&"
+URLEncoder.encode("phone","UTF-8")+"="+URLEncoder.encode(txtPhone,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while ((line = bufferedReader.readLine())!= null){
result+=line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
loading = ProgressDialog.show(context, "Loading Menu", "Please wait...", false, false);
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
// alertDialog.setMessage(result);
// alertDialog.show();
if (result.contains("Successfully")) {
loading.dismiss();
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
Intent i2 = new Intent(context, Login.class);
context.startActivity(i2);
} else if(result.contains("Congratulations")) {
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
Intent i = new Intent(context, MainActivity.class);
context.startActivity(i);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}