代码之家  ›  专栏  ›  技术社区  ›  Stanley Mungai

发送Android短信前提示

  •  1
  • Stanley Mungai  · 技术社区  · 12 年前

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

    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 回复  |  直到 12 年前
        1
  •  2
  •   Robby Pond    12 年前

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

        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();
    
        2
  •  1
  •   Bhavit S. Sengar    12 年前

    这将对您有效:

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