开发者

Unwanted automated creation of new instances of an activity class

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 = PendingInte开发者_StackOverflownt.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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜