2012-08-12 98 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 
    } 
}