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?