2
private void sendSMS(String phoneNumber, String message)
{
        PendingIntent pi = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String msg = "some text";
    sendSMS("xxxxxxxxx", msg);
}

I've got code like above. The problem is that when I run the application it sends one message after another. How to make it send one and only one sms?

korek
  • 113
  • 11

1 Answers1

0

I assume the this is in your MainActivity class. When you send the PendingIntent you send it to the same activity. The activity is then recreated and onCreate is called again.

If you really want to send the SMS in onCreate then send the Intent to another Activity. Otherwise create a button and send the SMS in the onClick event of the button.

AKroell
  • 622
  • 4
  • 18