在我的應用程序中,我使用調用AsyncTask-A
來檢查用戶是否是新用戶,並致電AsyncTask-B
提示用戶輸入新密碼並重置它。我開始知道顯示對話框應該在onPreExecute()
上完成,然後在doInBackground()
上更改密碼(使用KSOAP
)。但是,問題在於alert.show();
不會等待,並且會立即調用doInBackground()中斷邏輯。這裏是代碼的相關部分:AsyncTask onPreExcute()中的對話框
private class AsyncTaskPasswordResetter extends AsyncTask<String, Void, Void>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
AlertDialog.Builder builder = new AlertDialog.Builder(login.this);
builder.setMessage("Please enter new password");
final EditText input = new EditText(login.this);
input.setText("");
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
SoapObject request = new SoapObject("http://tempuri.org/", "ChangeParentPassword");
request.addProperty(MyUtils.CreateProp("Username", userName, String.class));
request.addProperty(MyUtils.CreateProp("Password", password, String.class));
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11) {{dotNet = true;}};
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE("http://hannuveda.online/default.asmx");
try {
androidHttpTransport.call("http://tempuri.org/ChangeParentPassword", envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
if (response.toString().equals("OK")) {
setTextViewstatus("passed\n");
} else {
setTextViewstatus("failed\n");
isError = true;
}
} catch (Exception e) {
setTextViewstatus("error\n");
isError = true;
}
}
}
);
AlertDialog alert = builder.create();
alert.show();
input.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
}
@Override
protected Void doInBackground(String... params)
{
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(isError)
SetLoginButtonEnability(true);
setTextViewstatus("wait...");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(login.this, trackbus.class);
intent.putExtra("UserID", userID);
startActivity(intent);
}
}, 1000);
}
}
爲什麼你的AsyncTask中有UI邏輯?你如何做外面的UI邏輯並使用回調。 –
我調用AsyncTaskA來檢查用戶是否是新的,如果用戶是新的,我從它的onPostExecute()調用AsyncTaskB。在AsyncTaskB中,我需要提示用戶(使用對話框)獲取新密碼。任何提示如何使用你所說的邏輯來實現這一點? –