0

I have tried to send SMS to a specific number once a button is clicked. However, the app always sends a toast message 'SMS NOT SENT' when I click the button in the app.

I have added the SEND_SMS permission in the manifest file.

This is my code below:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
}

public void OnClickStart(View view) {


    String sms = "this is a message";
    String phoneNo = "*******";


    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNo, null, sms, null, null);
        Toast.makeText(getApplicationContext(), "SMS Sent!",
                Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
                "SMS NOT SENT!",
                Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

}
}
Jobin
  • 5,610
  • 5
  • 38
  • 53
Amrit Rai
  • 1
  • 1

0 Answers0