Problem with SmsManager

0

I have the following code to send an SMS with the text of an EditText, this within an Alert

ImageButton btnCancelar = (ImageButton) view.findViewById(R.id.btnCancelar);
    ImageButton btnOk = (ImageButton) view.findViewById(R.id.btnOk);
    EditText CampoTexto = (EditText) view.findViewById(R.id.CampoTexto);

    final String capturaTexto = CampoTexto.getText().toString();

    final Dialog localizar = new Dialog(this);
    localizar.requestWindowFeature(Window.FEATURE_NO_TITLE);
    localizar.setContentView(view);
    btnOk.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ///enviar msg
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage("+MEUNUMEROAQUI", null, capturaTexto, null, null);
            ///enviar msg
            localizar.dismiss();
            Toast.makeText(getApplicationContext(), "Aguarde retorno...", Toast.LENGTH_LONG).show();
        }
    });

When I put some text to be sent in SmsManager, it sends normal, but when I use the String to capture the text, the application closes. What am I doing wrong?

In logcat returns invalid message body as you can see, but I did not endend why. Is not the String correct?

  

06-13 21: 56: 03.653 25444-25444 / com.aci.acirastreamento   D / AndroidRuntime: Shutting down VM 06-13 21: 56: 03.654   25444-25444 / com.aci.acirastreamento E / AndroidRuntime: FATAL EXCEPTION:   main

     

Process: com.aci.acirastreamento, PID: 25444                                                                            java.lang.IllegalArgumentException: Invalid message body                                                                                at android.telephony.SmsManager.sendTextMessage (SmsManager.java:265)                                                                                at   com.aci.acitration.MainActivity $ 1.onClick (MainActivity.java:45)                                                                                at android.view.View.performClick (View.java:4785)                                                                                at android.view.View $ PerformClick.run (View.java:19884)                                                                                at android.os.Handler.handleCallback (Handler.java:739)                                                                                at android.os.Handler.dispatchMessage (Handler.java:95)                                                                                at android.os.Looper.loop (Looper.java:135)                                                                                at android.app.ActivityThread.main (ActivityThread.java:5343)                                                                                at java.lang.reflect.Method.invoke (Native Method)                                                                                at java.lang.reflect.Method.invoke (Method.java:372)                                                                                at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:905)                                                                                at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:700) '

    
asked by anonymous 14.06.2016 / 00:07

1 answer

0

Invalid message body

Probably your string is null and we'll do the following

ImageButton btnCancelar = (ImageButton) view.findViewById(R.id.btnCancelar);
ImageButton btnOk = (ImageButton) view.findViewById(R.id.btnOk);
final EditText CampoTexto = (EditText) view.findViewById(R.id.CampoTexto);



final Dialog localizar = new Dialog(this);
localizar.requestWindowFeature(Window.FEATURE_NO_TITLE);
localizar.setContentView(view);
btnOk.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        ///enviar msg
        String capturaTexto = CampoTexto.getText().toString();

        if (capturaTexto.equals("") || capturaTexto == null) {
            // sua string está null
        }else{
          SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage("+MEUNUMEROAQUI", null, capturaTexto, null, null);
        ///enviar msg
        localizar.dismiss();
        Toast.makeText(getApplicationContext(), "Aguarde retorno...", Toast.LENGTH_LONG).show();  
        }
    }
});

get the value of editText at the time of the button's click.

    
14.06.2016 / 03:06