发送短信Android前提示

时间:2014-02-26 15:17:52

标签: android prompt

我是Android新手,我正在尝试通过Android应用程序发送短信。我需要它,如果消息包含单词“MYSELF”提示用户是否要继续发送SMS我的方法是:

public void sendSmsByManager() {

        try {

            // Get the default instance of the SmsManager

            SmsManager smsManager = SmsManager.getDefault();

            smsManager.sendTextMessage(phoneNumber.getText().toString(),
            null,
            smsBody.getText().toString(),
            null,
            null);
             //If the SMS Contains the WORD MYSELF Prompt the User If they Want to send the SMS

            Toast.makeText(getApplicationContext(),
                    "Your sms has successfully sent!",

                    Toast.LENGTH_LONG).show();

        } catch (Exception ex) {

            Toast.makeText(getApplicationContext(), "Your sms has failed...",

            Toast.LENGTH_LONG).show();

            ex.printStackTrace();

        }

    }

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:2)

创建警报确认样式对话框。

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Some Message");
    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               //send text
           }
       });
    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               //discard text
           }
       });

    AlertDialog dialog = builder.create();
    dialog.show();

答案 1 :(得分:1)

这对你有用:

public void sendSmsByManager() {
    try {

         //If the SMS Contains the WORD MYSELF Prompt the User If they Want to send the SMS
       if (smsBody.getText().toString().contains("MYSELF")){

           AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setMessage("SMS CONTAINS MYSELF! Do you really Want to send this SMS"); // It will set the message you want to display

            builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                    // Get the default instance of the SmsManager
                       SmsManager smsManager = SmsManager.getDefault();

                       smsManager.sendTextMessage(phoneNumber.getText().toString(),
                       null,
                       smsBody.getText().toString(),
                       null,
                       null);

                       Toast.makeText(getApplicationContext(),
                               "Your sms has successfully sent!",

                               Toast.LENGTH_LONG).show();
                   }
               });
            builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {

                       dialog.dismiss();

                   }
               });

            AlertDialog dialog = builder.create();
            dialog.show();

       }



    } catch (Exception ex) {

        Toast.makeText(getApplicationContext(), "Your sms has failed...",

        Toast.LENGTH_LONG).show();

        ex.printStackTrace();

    }

}