tags:

views:

39

answers:

3

I have an activity (called Sender) with the most basic UI, only a button that sends a message when clicked. In the onClickListener I only call this method:

private void sendSMS(String msg)
    {
     PendingIntent pi = PendingIntent.getActivity(this, 0, 
      new Intent(this, Sender.class), 0);
     PendingIntent pi = PendingIntent.getActivity(this, 0, myIntent, 0);
     SmsManager sms = SmsManager.getDefault();
     sms.sendTextMessage("1477", null, msg, pi, null);
    }

This works ok, the message is sent but every time a message is sent a new instance of Sender is started on top of the other. If I call sendSMS method three times, three new instances are started. I'm quite new to android so I need some help with this, I only want the same Sender to be on all the time

A: 

Try adding android:launchMode="singleTask" to your activity entry in the manifest. More information can be found here.

RickNotFred
+1  A: 

That PendingIntent parameter to sendTextMessage is intended for sending feedback to a component of your application, reporting whether the SMS was sent successfully.

Normally you should create a PendingIntent that launches a broadcast rather than an activity.

If you don't care whether the SMS was sent successfully, then just pass in null instead of pi.

Christopher
A: 

Thank you both for your answers. Christopher's solution works, the problem is SOLVED.

Marko
Eh? Click the accept button on it then. He doesn't get points otherwise. And don't post non-answers as answers.
Chris Dennett