简体   繁体   中英

Sending SMS from inner class using SmsManager is not working

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 of MainActivity.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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM