3

I'm developing an app that sends sms and I'able to get a response to know if the sms was sent (or not). But since I want to identify which sms was sent (by receiver and text) I add these informations to the intent using extras.

That works fine, but only for the first message I sent. After the first message, I receive the same extras and it seems I sent always the same sms. Actually each sms is sent correctly, so the problem regards only the "sent notification". The strange thing is that when I reiceive another sms it works fine again (I get the correct extras) but only for one sms. It seems that a new sms event "resets" the broadcast receiver of sent messages.

This is my code:

class SmsSender{
private final String SENT = "SMS_SENT";
private static SmsSender instance=null;
private BroadcastReceiver br;
SmsSender(){}

    //SINGLETON
public static SmsSender getSmsSender(Context ctx, AddressBook ab, DBManager dbm){
    if(instance==null && ctx!=null){
    instance=new SmsSender();
    instance.ctx=ctx;
    instance.ab=ab;
    instance.dbm=dbm;
    instance.setBR();
    }
    return instance;
}

private void setBR(){
    br=new BroadcastReceiver(){
         public void onReceive(Context arg0, Intent arg1) 
            {  
                br=null;

                switch (getResultCode())                
                {              
                case Activity.RESULT_OK:  
                    if(arg1.hasExtra("text") && arg1.hasExtra("receiver")){
                            Log.i("SMS","SMS sent to "+arg1.getStringExtra("receiver")+": "+arg1.getStringExtra("text"));
                    }
                break;
                    .....
                 }
            }
        };

        ctx.registerReceiver(br, new IntentFilter(SENT));

}

public void send(String phoneNumber, String text) {
    String nums[]=phoneNumber.split(",");


    for(int i=0; i<nums.length; ++i){
    if(nums[i]!="" && text!=""){
                SmsManager sms = SmsManager.getDefault();

                ArrayList<String> parts = sms.divideMessage(text);
                messageCount = parts.size();
                ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(messageCount);
                Intent myIntent=new Intent(SENT);

                **myIntent.putExtra("receiver", nums[i]);
                myIntent.putExtra("text", text);**

                for (int j = 0; j < messageCount; j++){
                    sentIntents.add(PendingIntent.getBroadcast(ctx, 0, myIntent,0));
                }

                    sms.sendMultipartTextMessage( nums[i].trim(), null, parts, sentIntents, null);
            }else{
            Notifica.notify(ab.getContactName(nums[i])+": Error sending SMS",ctx);
            }}


 }}

What is wrong?

supergiox
  • 1,586
  • 7
  • 19
  • 27
  • you send different text with multiple calls to `send()` and get the wrong text back or you send a message to multiple receivers and the intent has always the first number? – zapl Apr 29 '12 at 12:17
  • I don't see any error in your code. Maybe it is a bug in Android. – zapl Apr 29 '12 at 12:42
  • And can I solve it in another way? I haven't found any example but I think is a common situation... Any app that sends sms should get a notification imho – supergiox Apr 29 '12 at 12:47

1 Answers1

4

Finally I solved the problem just replacing this line:

sentIntents.add(PendingIntent.getBroadcast(ctx, 0, myIntent,0));

with this one:

sentIntents.add(PendingIntent.getBroadcast(ctx, 0, myIntent,PendingIntent.FLAG_UPDATE_CURRENT));

From the documentation:

FLAG_UPDATE_CURRENT ...if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.

supergiox
  • 1,586
  • 7
  • 19
  • 27
  • 1
    If you just update current extra data, so what if many SMSes was sent and come into your br(BroadcastReceiver) at the same time? You will lost the previous smses' extra data I think. – Wayne Jun 10 '12 at 02:58
  • @Supergiox I am in the same situation now. I tried your solution but the current data replaced the first one.. What will be the solution any idea – vinothp Aug 22 '12 at 13:53
  • @Wayne I am in the situation what you exactly said..Do you have any idea about how to resolve that issue – vinothp Aug 22 '12 at 13:54
  • 1
    i had the same problem, it can be solved by using the requestCode paramenter in PendingIntent.getBroadcast(). in the docs they state here http://developer.android.com/reference/android/app/PendingIntent.html "A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra"" and "you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent.filterEquals, or different request code integers supplied to ... getBroadcast ... " – memical Aug 07 '14 at 18:56