0

UPDATE: if I take sendSms() function outside the inner class it works! but I need it inside. Can someone help?

I'm trying to send sms in backgroud using SmsManager and nothing happens. when I go to Logcat it says:

E/art: Failed sending reply to debugger: Broken pipe

I've tested it on both emulator and real device

This is a part ofMainActivity.java:

 SmsReceiver.bindListener(new SmsListener() {
     @Override
     public void messageReceived(String messageText, String sender) {
          if (msgClassifier.isUrgent(messageText, null, null)) {
               sendNotification(messageText);
          }
          else {
               if(sharedPrefs.getAutoReplyState(getApplication())){
                    Toast.makeText(MainActivity.this, "send sms", Toast.LENGTH_SHORT).show();
                    sendSms(sender,messageText);

               }

          }
     }
 });




 public void sendSms(String number, String msg){
     android.telephony.SmsManager smsManager = SmsManager.getDefault();
     smsManager.sendTextMessage(number,null,msg,null,null);
 }

The Toast is being showed and I have also printed sender and messageText and it prints what it should print so this is not the problem. I have been looking for this error and tried to clean project, rebuild, exit android and nothing worked.

I have included SEND_SMS permission in Manifest

Yael Pesso
  • 53
  • 8

1 Answers1

0

Try This:

public void sendSMS(String phoneNo, String msg) {
    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNo, null, msg, null, null);
        Toast.makeText(getApplicationContext(), "Message Sent",
                Toast.LENGTH_LONG).show();
    } catch (Exception ex) {
        Toast.makeText(getApplicationContext(),ex.getMessage().toString(),
                Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    }
}
  • I realized that if I take sendSms function out of the inner class it works. Does anyone know how Can I still leave it inside? – Yael Pesso Sep 27 '18 at 11:03
  • I suspect you're probably using a static function that's why it won't let you sendSms within the function. Have you checked on this `SmsManager.getDefault()` part if taken out? – zhack Sep 27 '18 at 11:13
  • sendSMS is not static and it us called from onCreate. I have tried to put SmsManager.getDefault() out and it didn't help – Yael Pesso Sep 27 '18 at 13:20