我在Android应用程序上成功发送短信后尝试更新服务器数据库。
我使用循环根据我拥有的长度数据发送短信。
仅供参考:noHP是收件人编号,pesan是邮件。检查此table db
for (int j = 0; j < id.size(); j++)
{
sendSMS(noHp.get(j), pesan.get(j), id.get(j)); //looping sendSMS
}
这是我的sendSMS方法
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);
}
}
之后,我想基于本地数据库更新服务器数据库,代码是这样的
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());
}
但我遇到的问题是updateOutbox()
方法已执行,尽管sendSMS()
尚未完成。我想在SMS成功发送状态成功/失败后更新数据库。
任何建议都将不胜感激。谢谢:))
答案 0 :(得分:0)
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();
updateOutbox(//requeiredValues);
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));
希望有所帮助
答案 1 :(得分:0)
DataFrame[words: array<string>]
是异步的,该方法最初完成,可能会迟到。
为什么不将sendSMS
置于异步方法中?
updateOutbox()
答案 2 :(得分:0)
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() {
String stts="", notes="";
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
//col.insert(new outboxlocal(id, df.format(c.getTime()), "SMS Sent", "-"));
stts = "SMS Sent";
notes = "-";
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
stts = "Not Sent";
notes = "Generic Failure";
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
//col.insert(new outboxlocal(id, df.format(c.getTime()), "Not Sent", "No Service"));
stts = "Not Sent";
notes = "No Service";
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
//col.insert(new outboxlocal(id, df.format(c.getTime()), "Not Sent", "Null PDU"));
stts = "Not Sent";
notes = "Null PDU";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
//col.insert(new outboxlocal(id, df.format(c.getTime()), "Not Sent", "Radio Off"));
stts = "Not Sent";
notes = "Radio Off";
break;
}
updateOutbox(id, username, AppKey, df.format(c.getTime()), stts, notes); //mengupdate info SMS ke tabel sms_outbox server
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
getBaseContext().registerReceiver(new BroadcastReceiver() {
String stts="", notes="";
@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", "-"));
stts = "SMS Delivered";
notes = "-";
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"));
stts = "Not Sent";
notes = "Failed, SMS not sent";
break;
}
updateOutbox(id, username, AppKey, df.format(c.getTime()), stts, notes);
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message); //memasukkan isi SMS ke dalam array menjadi beberapa part
if (parts.size() == 1) { //jika hanya 1 part, yang bearti isi SMS < 160 karakter
String msg = parts.get(0);
sms.sendTextMessage(phoneNumber, null, msg, sentPI, deliveredPI);
} else { //jika isi SMS > 160 karakter
ArrayList<PendingIntent> sentPis = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> delPis = new ArrayList<PendingIntent>();
int ct = parts.size();
for (int i = 0; i < ct; i++) { //looping send sms berdasarkan jumlah part
sentPis.add(i, sentPI);
delPis.add(i, deliveredPI);
}
sms.sendMultipartTextMessage(phoneNumber, null, parts, sentPis, delPis);
}
}