Here is my code
Log.i("sdfgsdf", time.toGMTString());
String SENT = "SMS_SENT";
Intent y= new Intent(SENT);
y.putExtra("number", phoneNumber);
y.putExtra("time", time.toString());
String DELIVERED = "SMS_DELIVERED";
int FLAG_UPDATE_CURRENT =(0x08000000) ;
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
y, FLAG_UPDATE_CURRENT);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), FLAG_UPDATE_CURRENT);
//---when the SMS has been sent---
r= new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent"+arg1.getStringExtra("number"),
Toast.LENGTH_SHORT).show();
Constant.DB = Compose.this.openOrCreateDatabase("Indigo",MODE_PRIVATE, null);
Constant.DB.execSQL("UPDATE Message SET IsMsgSent='true' where DateAndTime='" + arg1.getStringExtra("time") + "' and ContactNumber='"+arg1.getStringExtra("number")+"'");
Constant.DB.execSQL("UPDATE Message SET FolderId='5' where DateAndTime='" + arg1.getStringExtra("time") + "' and ContactNumber='"+arg1.getStringExtra("number")+"'");
Constant.DB.close();
// unregisterReceiver(r);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure"+arg1.getStringExtra("number"),
Toast.LENGTH_SHORT).show();
Constant.DB = Compose.this.openOrCreateDatabase("Indigo",MODE_PRIVATE, null);
Constant.DB.execSQL("UPDATE Message SET IsMsgSent='false' where DateAndTime='" + arg1.getStringExtra("time") + "' and ContactNumber='"+arg1.getStringExtra("number")+"'");
Constant.DB.close();
// unregisterReceiver(r);
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service"+arg1.getStringExtra("number"),
Toast.LENGTH_SHORT).show();
Constant.DB = Compose.this.openOrCreateDatabase("Indigo",MODE_PRIVATE, null);
Constant.DB.execSQL("UPDATE Message SET IsMsgSent='false' where DateAndTime='" + arg1.getStringExtra("time") + "' and ContactNumber='"+arg1.getStringExtra("number")+"'");
Constant.DB.close();
// unregisterReceiver(r);
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU"+arg1.getStringExtra("number"),
Toast.LENGTH_SHORT).show();
Constant.DB = Compose.this.openOrCreateDatabase("Indigo",MODE_PRIVATE, null);
Constant.DB.execSQL("UPDATE Message SET IsMsgSent='false' where DateAndTime='" + arg1.getStringExtra("time") + "' and ContactNumber='"+arg1.getStringExtra("number")+"'");
Constant.DB.close();
// unregisterReceiver(r);
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off"+arg1.getStringExtra("number"),
Toast.LENGTH_SHORT).show();
Constant.DB = Compose.this.openOrCreateDatabase("Indigo",MODE_PRIVATE, null);
Constant.DB.execSQL("UPDATE Message SET IsMsgSent='false' where DateAndTime='" + arg1.getStringExtra("time") + "' and ContactNumber='"+arg1.getStringExtra("number")+"'");
Constant.DB.close();
// unregisterReceiver(r);
break;
}
}
};
registerReceiver(r, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
And when i send multiple sms using a for loop iam getting extras of only last sent sms.how to get all extras..?
This should get all values
Bundle bundle = intent.getExtras();
for (String key : bundle.keySet()) {
Object value = bundle.get(key);
Log.d(TAG, String.format("%s %s (%s)", key,
value.toString(), value.getClass().getName()));
}
Calling intent place do it like this
Bundle bundle = new Bundle();
bundle.putString("RetailerName", keyword);
bundle.putString("positon", aString);
intent.putExtras(bundle);
receive intent place get the data like this
Bundle bundle = this.getIntent().getExtras();
String RetailerName = bundle.getString("retailerName");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With