1

我想实现一个注册活动,用户插入他/她的信息,然后单击一个按钮将此信息发送到 Web 服务,该 Web 服务将此信息存储在数据库中。

我将用于连接到 Web 服务的代码放在一个单独的线程中(不在 UI 线程中),并且我想显示一个progressdialog直到与 Web 服务的连接完成,然后我想显示一个AlertDialog以显示不同的消息,例如(使用此电子邮件尝试不同的,或注册成功!)

这是用户单击注册按钮时的借口:

public void SignupNewUser (View V)
{
    Working = ProgressDialog.show(this, "Working..", "Connecting To Server");       
    Runnable work = new Runnable() {
        @Override
        public void run() {
            Edit_Text_FName = (EditText) findViewById(R.id.Edit_Text_Fname_Signup);
            Edit_Text_LName = (EditText) findViewById(R.id.Edit_Text_Lname_Signup);
            Edit_Text_Password = (EditText) findViewById(R.id.Edit_Text_Password_Signup);
            Edit_Text_Email = (EditText) findViewById(R.id.Edit_Text_Email_Signup);
            S1 = (Spinner) findViewById(R.id.Spinner_Signup);
            SignupPerson SUPerson = new SignupPerson();
            SUPerson.F_Name = Edit_Text_FName.getText().toString().trim();
            SUPerson.L_Name = Edit_Text_LName.getText().toString().trim();
            SUPerson.E_Mail = Edit_Text_Email.getText().toString().trim();
            SUPerson.PassW  = Edit_Text_Password.getText().toString().trim();
            SUPerson.Gen = Choosen_Gender;
            SUPerson.Cou_Id = S1.getSelectedItemPosition();
            METHOD = "signup";
            SoapObject Request = new SoapObject(NAMESPACE, METHOD);
            PropertyInfo P = new PropertyInfo();
            P.setName("SUPerson");
            P.setValue(SUPerson);
            P.setType(SUPerson.getClass());
            Request.addProperty(P);
            SoapSerializationEnvelope envolope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
            envolope.dotNet = true;
            envolope.setOutputSoapObject(Request);
            envolope.addMapping(NAMESPACE, "SignupPerson", new SignupPerson().getClass());
            HttpTransportSE ahttp = new HttpTransportSE(URL);
            SoapPrimitive Res = null;
            try
            {
                ahttp.call(NAMESPACE+METHOD, envolope);
                Res = (SoapPrimitive) envolope.getResponse();
            }
            catch (Exception ex)
            {
                //ex.printStackTrace();
                result = -1;
            }
            if (result != -1)
            {
                result = Integer.parseInt(Res.toString());
            }
            Working.dismiss();
        }
    };
    Thread SS = new Thread(work);
    SS.start();
    switch (result)
    {
    case -1:
        showDialog(-1);
        break;
    case 0:
        showDialog(0);
        break;
    case 1:
        showDialog(1);
        break;
    case 2:
        showDialog(2);
        break;
        default:break;
    }   
}

@Override
protected Dialog onCreateDialog(int id)
{

    switch (id)
    {
    case -1:
        return new AlertDialog.Builder(this)
        .setTitle("error!")
        .setMessage("error connecting to the server. please try again")
        .setIcon(R.drawable.ic_error)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub

            }
        })
        .create();
    case 0:
        return new AlertDialog.Builder(this)
        .setTitle("error!")
        .setMessage("You have entered an Exists Email, Please try another one")
        .setIcon(R.drawable.ic_error)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
            }
        }).create();
    case 1:
        return new AlertDialog.Builder(this)
        .setTitle("error!")
        .setMessage("Server Error, Please Try Again Later")
        .setIcon(R.drawable.ic_error)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
            }
        })
        .create();
    case 2:
        return new AlertDialog.Builder(this)
        .setTitle("Registration successfully!")
        .setMessage("Click OK to Sign in and Start Usign Hello!!")
        .setIcon(R.drawable.ic_success)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                Intent i = new Intent(SignupActivity.this ,MainActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
                startActivity(i);
            }
        })
        .create();
    }
    return null;
}

这里,SUPerson是一个保存用户信息的对象,result是一个整数,表示AlertDialog连接到Web服务端后将显示的内容。

我的问题是,当我运行上面的代码时,没有出现警报对话框消息!为什么 ?

4

1 回答 1

0

如果你使用AsyncTask,你会更容易做到这一点。我认为你可能没有得到你的对话框,因为你试图在启动线程后立即显示它。

使用 AsyncTask,您可以让您的服务器连接在单独的线程上的 doInBackground() 中运行,然后您可以在 onPostExecute() 中调用您的对话框。

让我知道这是否有意义!该链接非常清楚地说明了如何使用它。:)

编辑:我还想提一下,如果您使用 AsyncTask,它可以让您轻松地在 onProgressUpdate() 方法中设置 ProgressDialog。

于 2013-10-07T15:02:09.570 回答