I am trying to update server database once I have successfully sent SMS on Android application.
I use a loop to send SMS based on the length data I have.
FYI : noHP is recipient number, pesan is message. check this table db
for (int j = 0; j < id.size(); j++)
{
sendSMS(noHp.get(j), pesan.get(j), id.get(j)); //looping sendSMS
}
And this is my sendSMS method
private void sendSMS(String phoneNumber, String message, final int id) {
col = new ControllerOutboxLocal(getApplicationContext());
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
strdate = date.format(new Date());
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
getBaseContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
col.insert(new outboxlocal(id, df.format(c.getTime()), "SMS Sent", "-")); //insert id, timesent, sms status into local database
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getApplicationContext(), "GENERIC FAILURE", Toast.LENGTH_SHORT).show();
col.insert(new outboxlocal(id, df.format(c.getTime()), "Not Sent", "Generic Failure"));
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
col.insert(new outboxlocal(id, df.format(c.getTime()), "Not Sent", "No Service"));
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
col.insert(new outboxlocal(id, df.format(c.getTime()), "Not Sent", "Null PDU"));
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
col.insert(new outboxlocal(id, df.format(c.getTime()), "Not Sent", "Radio Off"));
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
getBaseContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getApplicationContext(), "SMS Delivered", Toast.LENGTH_SHORT).show();
co.insert(new outbox(1, strdate));
col.insert(new outboxlocal(id, df.format(c.getTime()), "SMS Delivered", "-"));
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getApplicationContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
col.insert(new outboxlocal(id, df.format(c.getTime()), "Not Sent", "Failed, SMS not sent"));
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
if (parts.size() == 1) {
String msg = parts.get(0);
sms.sendTextMessage(phoneNumber, null, msg, sentPI, deliveredPI);
} else {
ArrayList<PendingIntent> sentPis = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> delPis = new ArrayList<PendingIntent>();
int ct = parts.size();
for (int i = 0; i < ct; i++) {
sentPis.add(i, sentPI);
delPis.add(i, deliveredPI);
}
sms.sendMultipartTextMessage(phoneNumber, null, parts, sentPis, delPis);
}
}
and after that, i want to update server database based on local database, the code is like this
for(int j=0; j<col.getAll().size(); j++)
{
updateOutbox(col.getAll().get(j).getId(), username, AppKey, col.getAll().get(j).getTimesent(), col.getAll().get(j).getStatus(), col.getAll().get(j).getNotes());
}
but the problem that I have is, updateOutbox()
method is executed although sendSMS()
has not finished yet. I want to update DB after SMS has sent successfully with status success/failed.
Any suggestion would be appreciated. thanks :)