-1

我是安卓新手。我想在用户单击登录按钮时显示进度对话框。我试过了,但对话框没有显示

btn_logIn.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View view) {
         getUserCredentials();
     }
}); //end of anonymous class

private void showProgressDialog() {     
    if (dialog == null) {       
        dialog = new ProgressDialog(this);          
    }
    dialog.setMessage("Please Wait. Your authentication is in progress");
    dialog.setButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which)              
            dialog.dismiss();               
        }           
    }); //end of anonymous class        
    dialog.show();  
} //end of showProgressDialog()

private void getUserCredentials() {

    EditText txt_userName = (EditText) findViewById(R.id.txt_userName);
    String userName = txt_userName.getText().toString();

    EditText txt_password = (EditText) findViewById(R.id.txt_password);
    String password = txt_password.getText().toString();

    if (userName != null && !userName.trim().equals("") && password != null && !password.trim().equals("")) {                   
        showProgressDialog();       
        callWebService(userName, password);         
    }
} //end of getUserCredentials()

private void callWebService(String userName, String password) {

    try {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("userName", userName);
        ....            
        Object result = envelope.getResponse();             
        if (result.equals("true")) {                
            dialog.dismiss();
            Toast.makeText(this, result.toString(), Toast.LENGTH_SHORT).show();

        } else {                
            dialog.dismiss();
            Toast.makeText(this, result.toString(), Toast.LENGTH_SHORT).show();                             
        }

     } catch (SocketTimeoutException e) {            
         dialog.dismiss();
         Toast.makeText(this, "Service is not connected, Please make sure your server is running", Toast.LENGTH_LONG).show();                       
    }  catch(Exception e) {         
        e.printStackTrace();
        dialog.dismiss();
        Toast.makeText(this, "Unable to connect, please try again later. Thank you", Toast.LENGTH_LONG).show();                     
     }
} //end of callWebServide()

我做错什么了吗。当我单击登录按钮并且服务未运行时,它显示消息“服务未连接,请确保您的服务器正在运行”,但对话框未显示...为什么?我的逻辑是用户单击登录时按钮和字段有值然后开始显示进度对话框,如果发生任何事情,例如结果出现或服务器未运行或发生任何异常,然后我删除对话框并显示适当的消息,但对话框没有显示...为什么?我做错了什么?请帮助..

谢谢

4

2 回答 2

0

当任务启动时使用 AsyncTask 初始化您的小部件,然后从 run 方法调用您的 web 服务并在 stop 方法上关闭您的进度条。

于 2012-06-29T05:42:33.303 回答
0

试试这个,

像这样更改您的 getUserCredentials(),

private void getUserCredentials() {

    EditText txt_userName = (EditText) findViewById(R.id.txt_userName);
    String userName = txt_userName.getText().toString();

    EditText txt_password = (EditText) findViewById(R.id.txt_password);
    String password = txt_password.getText().toString();

    if (userName != null && !userName.trim().equals("") && password != null && !password.trim().equals("")) {                   
        showProgressDialog();
 Thread t=new Thread(new Runnable() {

        public void run() {
             callWebService(userName, password);  

        }
    });   t.start();    
  }
}

而你的callWebService方法是这样的,

private void callWebService(String userName, String password) {

try {

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("userName", userName);
    ....            
    Object result = envelope.getResponse();             
    if (result.equals("true")) {                
        dialog.dismiss();
        Toast.makeText(this, result.toString(), Toast.LENGTH_SHORT).show();

    } else {  
        ActivityName.this.runOnUiThread(new Runnable() {

    public void run() {
         dialog.dismiss();
        Toast.makeText(this, result.toString(), Toast.LENGTH_SHORT).show();  

    }
});              

    }

 } catch (SocketTimeoutException e) {  
      ActivityName.this.runOnUiThread(new Runnable() {

    public void run() {
          dialog.dismiss();
     Toast.makeText(this, "Service is not connected, Please make sure your server is running", Toast.LENGTH_LONG).show();                       
    }
});           

}  catch(Exception e) {         
    e.printStackTrace();
 ActivityName.this.runOnUiThread(new Runnable() {
    public void run() {
           dialog.dismiss();
    Toast.makeText(this, "Unable to connect, please try again later. Thank you", Toast.LENGTH_LONG).show();                       
    }
});   

 }

}

更新 1

要从您的评论中回答您的问题,

1)是的异步任务更有效。它有自己的方法来完成我在此处描述的相同任务。

AsyncTask 有以下方法,

i)onPreExecute()-可用于启动对话框 ii)doInBackground()-充当后台线程。iii)onPostExecute()-在结束时调用它,您可以在其中关闭对话框。

我没有提到的原因是,您可能必须更改工作代码的结构以适应异步任务。

2) runonUiThread - 顾名思义,其中的任何内容都将被视为在主 UI 线程中运行。所以基本上要更新屏幕,你必须使用它。还有其他可用的方法,例如也可以执行相同任务的处理程序。

于 2012-06-29T05:55:12.133 回答