0

私のアンドロイドでは、メッセージボックスを表示して終了する方法がなく、表示中に別のプロセスを実行する必要があります (私の場合はメール送信機能)。次に、メールの送信が完了したら、アラート ボックスを閉じる必要があります。

これは私がこれまでに得たものですが、機能していません...

誰でも助けることができますか?

    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();
        } 
    });
4

2 に答える 2

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 
   }
}
于 2012-08-13T00:08:28.280 に答える
2

メール送信の部分はよくわかりませんが、メッセージボックスを希望通りにするお手伝いはできます。

次のコードを削除した場合:

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

次に、ダイアログはボタンなしで表示され、追加.setCancelable(false)すると、指示するまで閉じられません。alertDialog.cancel();

これが例です(私のダイアログの1つから変更されました):

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();
    }
}
于 2012-08-13T00:17:18.607 に答える