私のアプリでは、AsyncTask-A
という呼び出しを使用して、新しいユーザーであるかどうかを確認し、AsyncTask-B
を呼び出してユーザーに新しいパスワードの入力を要求してリセットします。ダイアログを表示するにはonPreExecute()
を実行してから、KSOAP
を使用してパスワードをdoInBackground()
に変更してください。しかし、問題は、alert.show();
が待たずに、doInBackground()がすぐにロジックを壊すことになるということです。ここでは、コードの関連部分は次のとおりです。Dialog in AsyncTask onPreExecute()
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では、(ダイアログを使用して)ユーザーに新しいパスワードを入力する必要があります。あなたが言ったロジックを使ってこれを実装する方法に関するヒント? –