0

This code is not working.

I have included <uses-permission android:name="android.permission.SEND_SMS"/> in manifeast.

public class MainActivity extends AppCompatActivity {

Button send;
EditText phoneNo;
EditText sms;
SmsManager smsManager;

void sendMe(View view){
   sendMessage();

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void sendMessage() {

    phoneNo = (EditText) findViewById(R.id.PhoneNo);
    sms = (EditText) findViewById(R.id.sms);

    String Number = phoneNo.getText().toString();
    String message = sms.getText().toString();
    try {
       smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(Number, "Number", message, null, null);
        Toast.makeText(this,"SMS SEND", Toast.LENGTH_SHORT).show();
    }catch(Exception e){
        Toast.makeText(this, "SMS FAILED", Toast.LENGTH_SHORT).show();
    }
  }
}
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30

1 Answers1

0

You haven't posted the exception, so I cannot be 100% sure, but I'm assuming that this iis failing due to the 2nd parameter in this call:

smsManager.sendTextMessage(Number, "Number", message, null, null);

The second parameter is the phone number of the SMSC. In general, unless you are using a custom SMSC you should just provide null as the second parameter to use the default SMSC.

David Wasser
  • 93,459
  • 16
  • 209
  • 274