Currently I'm building a Android SMS Gateway. So far everything works fine. The issue is that when I send a SMS on the Database List loop for Sending SMS. When I receive the SmsSentReceiver Broadcast its always the same ID.
I must mention that I've the proper Permissions on the Manifest.xml SEND,RECEIVE,PHONE_STATE, INTERNET
Here is my SendSMS Method:
public String sendSMS(String phoneNumber, String message, Long RowId) {
Log.e(TAG,"SENDING ROW ID: "+RowId);
String SENT = "SMS_SENT";
Intent intentSent = new Intent(SENT);
intentSent.putExtra("RowId", RowId);
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, intentSent, 0);
/*String DELIVERED = "SMS_DELIVERED";
Intent intentDelivered = new Intent(DELIVERED);
intentDelivered.putExtra("RowId", RowId);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, intentDelivered, 0);*/
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, null); //deliveredPI
return "SMS:";
}
I've this on my Manifest.xml
<service android:name=".services.Smsd"></service>
<receiver android:name=".services.SmsSentReceiver">
<intent-filter>
<action android:name="SMS_SENT" />
</intent-filter>
</receiver>
my SmsSentReceiver.java
public class SmsSentReceiver extends BroadcastReceiver {
private static final String TAG = "SmsSentReceiver";
@Override
public void onReceive(Context context, Intent arg1) {
Log.e(TAG, "++++++++++++++++++++++++++ SmsSentReceiver "+ arg1.getExtras().getLong("RowId") +" +++++++++++++++++++++++++++++++++++++++++");
if (getResultCode() == Activity.RESULT_OK) {
try {
Log.e(TAG, "Sent ...");
Outbox outbox = SugarRecord.findById(Outbox.class,arg1.getExtras().getLong("RowId"));
Log.e(TAG, "RESULT_OK");
Log.e(TAG, String.format("Phone:%s \nBody:%s \nSmsId:%s \nExtra:%s \nLocalExtra:%s \nStatus:%s",
outbox.getPhone(),
outbox.getBody(),
outbox.getSmsId(),
arg1.getExtras().getLong("RowId"),
outbox.getId(),
outbox.getStatus()));
outbox.Status = "1"; // 1 =success
outbox.SendingDate = new Date();
outbox.save();
// Delete SMS
context.getContentResolver().delete(Uri.parse("content://sms/inbox"), outbox.getBody(), new String[]{"body"});
} catch (Exception e) {
Log.e(TAG, "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
Log.e(TAG, "SmsSentReceiver ERROR: " + e.toString());
Log.e(TAG, "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
}
} else {
try {
Log.e(TAG, "Not Sent ...");
Outbox outbox = SugarRecord.findById(Outbox.class,arg1.getExtras().getLong("RowId"));
Log.e(TAG, "RESULT_CANCELED");
Log.e(TAG, String.format("Phone:%s \nBody:%s \nSmsId:%s \nExtra:%s \nLocalExtra:%s \nStatus:%s",
outbox.getPhone(),
outbox.getBody(),
outbox.getSmsId(),
arg1.getExtras().getLong("RowId"),
outbox.getId(),
outbox.getStatus()));
outbox.Status = "2";
outbox.SendingDate = new Date();
outbox.Retries = outbox.getRetries() + 1;
outbox.save();
// Delete SMS
context.getContentResolver().delete(Uri.parse("content://sms/inbox"), outbox.getBody(), new String[]{"body"});
} catch (Exception e) {
Log.e(TAG, "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
Log.e(TAG, "SmsSentReceiver ERROR: " + e.toString());
Log.e(TAG, "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
}
*****solution***** Add RowId.intValue() Add PendingIntent.FLAG_ONE_SHOT
Set a Unique Request Code and Just run this Once. https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_ONE_SHOT
PendingIntent sentPI = PendingIntent.getBroadcast(this, RowId.intValue(), intentSent, PendingIntent.FLAG_ONE_SHOT);