2014-02-26 152 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

我編輯了我的答案。 –