2014-02-26 64 views
1

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

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

     } 

    } 

我怎样才能做到这一点?

+0

你想在用户发送文本对自己这个触发,当用户从字面上将消息发送到“我”或干脆当消息包含文字“我自己”? – Andreas

+0

当文本包含单词“我自己” – ErrorNotFoundException

回答

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(); 
+0

很抱歉,我不知道如何将它纳入我的代码?我想这是第一次:( – ErrorNotFoundException

+0

这将放入您的代码发送文本,然后在onClick为肯定按钮,您将移动发送呼叫 –

+0

我想在提示中的文本。包含MYSELF,你真的想发送?“ – ErrorNotFoundException

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

    } 

} 
+0

这带来了提示,但没有文字”SMS CONTAINS MYSELF!你真的想发送这条短信“ ? – ErrorNotFoundException

+0

我编辑了我的答案。 –