0

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);
Eddwin Paz
  • 2,842
  • 4
  • 28
  • 48
  • Pass a different request code - the second parameter in `getBroadcast()` - for each `PendingIntent`, instead of 0 for all of them. – Mike M. Oct 08 '17 at 07:32
  • @MikeM. you mean like this String SENT = "SMS_SENT"+RowId; ????? – Eddwin Paz Oct 08 '17 at 07:35
  • 1
    No. `PendingIntent.getBroadcast(this, «requestCode», intentSent, 0)` - Pass a different `int` there for each one. – Mike M. Oct 08 '17 at 07:36
  • @MikeM. But all this unique PendingIntent wont be there listening to that unique Broadcast forever knowing they wont get any data in the future? I'm Building a Loop that Queues SMS constantly wont it crash the app by consuming all memory? – Eddwin Paz Oct 08 '17 at 07:36
  • I'm not sure what you're saying. At any rate, you really don't want to use a straight loop to send multiple SMS. You can easily end up with a silent generic failure, and some messages just won't send. – Mike M. Oct 08 '17 at 07:39
  • @MikeM. 100% truth... ; How I can get this request code on my onReceive() ? – Eddwin Paz Oct 08 '17 at 07:40
  • If you want the request code there, you'll have to attach it as an extra. The `Intent` class doesn't keep track of it itself. – Mike M. Oct 08 '17 at 07:43
  • Sorry for my ignorance. But if I put this unique requestCode on the class I will get it as usual? also will this unique broadcast still alive listening for the same unique request code always? Im saying this imagine 1000 unique listening for nothing after I've listened for the pourpose of updating the database status of the true/false sent of the sms. – Eddwin Paz Oct 08 '17 at 07:46
  • "But if I put this unique requestCode on the class I will get it as usual?" - Yep. The request code serves only to differentiate `PendingIntent`s. The actual `Intent` fired is not affected by it. "also will this unique broadcast still alive listening for the same unique request code always?" - I'm not quite sure if I'm following you, but if you're concerned about cached `PendingIntent`s "stacking up", you can pass `PendingIntent.FLAG_ONE_SHOT` as the last argument in `getBroadcast()`, which will cause it to be cleared after use. – Mike M. Oct 08 '17 at 07:51
  • 1
    Thanks @MikeM. All Seem To work correctly. Gonna see the behavior of the FLAG to see if it works. But the SMS are updating properly now.. :D – Eddwin Paz Oct 08 '17 at 07:56

0 Answers0