-1

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 :)

HelmiB
  • 12,303
  • 5
  • 41
  • 68
Dwi Teguh Prasetyo
  • 179
  • 1
  • 3
  • 15

3 Answers3

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));

hope it helps

Mr.Popular
  • 845
  • 1
  • 8
  • 15
  • did you mean i have to add updateOutbox() into register receiver too? its not work, i've tried before – Dwi Teguh Prasetyo Apr 16 '17 at 08:24
  • have u got the toast that says sms sent.. if u got the toast means it shud update ur database and all.. check ur updatebox method debug it. there is nothing wrong in ur code – Mr.Popular Apr 16 '17 at 10:25
  • problem solved. i try to fix them with move the updateOutbox() method into onReceive() after switch-case. i believe that the problem come because maybe device need some delay before get Result of sms send – Dwi Teguh Prasetyo Apr 16 '17 at 13:26
0

sendSMS is asynchronous, the method is completed at first, and might response late.

why not put updateOutbox() inside asynchronous method ?

//---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

                   //CALL Update OutBox Here

                    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"));

                   //CALL Update OutBox Here

                    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"));

                    //CALL Update OutBox Here

                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    col.insert(new outboxlocal(id, df.format(c.getTime()), "Not Sent", "Radio Off"));

                    //CALL Update OutBox Here

                    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", "-"));

                    //CALL Update OutBox Here

                    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"));

                    //CALL Update OutBox Here

                    break;
            }
        }
    }, new IntentFilter(DELIVERED));
HelmiB
  • 12,303
  • 5
  • 41
  • 68
0

thanks to all who have responded to my question. now i solved this problem with change the sendSMS() method becomes the code below

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);
    }


}
Dwi Teguh Prasetyo
  • 179
  • 1
  • 3
  • 15