2012-08-12 58 views
0

在我的android中,我想显示一个消息框无法退出它,并显示时,另一个进程需要运行(在我的情况下,一个电子邮件发送函数)。然后在电子邮件完成发送后,警报框需要关闭。android如何显示和删除alertdialog

这是我得到了这么远,但它不工作...

谁能帮助?

AlertDialog.Builder alertDialog = new Builder(this); // create an alert box 
    alertDialog.setTitle("Sending..."); 
    alertDialog.setMessage("Please wait while your details and image is being sent to x."); 

    alertDialog.show(); // show the alert box 
    Reusable_CodeActivity.send_email(gmailAC, gmpassword, get_all_details(), image_path, this); 

    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { // define the 'OK' button 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
     } 
    }); 
+0

你是什么意思'不working'? – iTurki 2012-08-12 23:50:59

+0

它在警告框出现之前开始发送电子邮件。 – omega 2012-08-12 23:52:47

+0

您是否将电子邮件发送功能置于单独的线程中? – iTurki 2012-08-12 23:54:41

回答

2

我不知道的电子邮件发送的一部分,但我可以帮助你做出的消息框喜欢你想要。

如果删除下面的代码:

alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { // define the 'OK' button 
    public void onClick(DialogInterface dialog, int which) { 
     dialog.cancel(); 
    } 
}); 

然后对话框将显示,没有按键,如果你添加.setCancelable(false)它不会被解雇,直到你告诉它,使用alertDialog.cancel();

这里的一个示例(修改从我的对话中的一个):

AlertDialog.Builder builder = new AlertDialog.Builder(this); // Create the dialog object 
     builder.setMessage(R.string.dialog_disclaimer_text) // I use a reference to a string resource - it's good practice instead of using hardcoded text 
       .setIcon(android.R.drawable.ic_dialog_info) // Here I specify an icon to be displayed in the top corner 
       .setTitle(R.string.dialog_disclaimer_title) 
       .setCancelable(false) // This one makes the dialog stay until the dismiss is called 

       .create().show(); // Show the dialog 

该代码将显示一个对话框,文本,这将在活动调用builder.dismiss();之前不会消失 - 然后您必须在某种侦听器中实现该功能,或者在完成发送后进行回调。在对方的回答

更新

来看,这可能是你的代码应该怎么样子(感谢iturki)

private class SendEmailTask extends AsyncTask<Void, Void, Void> { 
    AlertDialog.Builder alertDialog; // Define the AlertDialog builder object so it can be used/adressed across the entire class 

    protected void onPreExecute() { 
     //Show the dialog first 
     alertDialog = new Builder(context); 
     alertDialog.setTitle("Sending...") 
        .setMessage("Please wait while your details and image is being sent to x."); 
        .setCancelable(false) 
        .show(); 
    } 
    protected void doInBackground(Void... params) { 
     //Send Email Code 
     Reusable_CodeActivity.send_email(gmailAC, gmpassword, get_all_details(), image_path, this); 
    } 

    protected void onPostExecute(Void result) { 
     //Dismiss the dialog 
     alertDialog.dismiss(); 
    } 
} 
2

这里是AsyncTask的结构简单,你可以使用:

private class SendEmailTask extends AsyncTask<Void, Void, Void> {  
    protected void onPreExecute() { 
     //Show the dialog first 
    } 
    protected void doInBackground(Void... params) { 
     //Send Email Code 
    } 

    protected void onPostExecute(Void result) { 
     //Dismiss the dialog 
    } 
}