how to sms multi number in android using pendingIntent?

256 Views Asked by At

I have problem in my source code, when I SMS multi numbber I just have deliver report which ID same ,it is the last number which was send

String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
    PendingIntent sentPI;
    PendingIntent deliverPI;

    try {
        JSONObject json = new JSONObject(content);
        status = json.getString("status");
        message = json.getString("message");
        pesan = json.getString("pesan");
        contacts = json.getJSONArray("contacts");   
        Log.i("INFO","status " + status + ", message " + message + ", pesan "+ pesan + " , jml contacts " + contacts.length());

        for (int i = 0; i < contacts.length(); i++) {
            JSONObject datauser = contacts.getJSONObject(i);
            Log.i("INFO", "no :" + (i + 1) + " " + datauser.getString(NO));

        }

        Intent sendinten = new Intent(SENT);
        Intent deliverIntent = new Intent(DELIVERED);

        SmsManager sms = SmsManager.getDefault();
        int[] id_recever = new Gson().fromJson(id_message, int[].class);

        for (int i = 0; i < id_recever.length; i++) {
            JSONObject datauser = contacts.getJSONObject(i);
            String id = String.valueOf(id_recever[i]);

            Log.i("INFO", "ID message " + id);
            deliverIntent.putExtra("id_send", id);

            deliverPI = PendingIntent.getBroadcast(getBaseContext(), 0,deliverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            sentPI = PendingIntent.getBroadcast(getBaseContext(), 0,sendinten, PendingIntent.FLAG_UPDATE_CURRENT);
            sms.sendTextMessage(datauser.getString(NO), null, pesan,sentPI, deliverPI);
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.d("DATA", "GAGAL");
    }

//-----------SMS Delivert -----------//

    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "SMS send",Toast.LENGTH_SHORT).show();
                Log.i("INFO", "SMS SEND");
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:// no salah
                Toast.makeText(getBaseContext(), "Generic failure",Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE: // didn't get service
                                                        // provider
                Toast.makeText(getBaseContext(), "No service",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU: //
                Toast.makeText(getBaseContext(), "Null PDU",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF: // mode airplane
                Toast.makeText(getBaseContext(), "Radio off",
                        Toast.LENGTH_SHORT).show();
                break;
            }

        }
    }, new IntentFilter(SENT));

//-----------SMS Delivert -----------//

    registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String id_message_send = intent.getStringExtra("id_send");
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Log.i("INFO", "SMS Delivered " + id_message_send);
                AsyncHttpClient postId = new AsyncHttpClient();
                RequestParams id = new RequestParams();
                id.put("id_message_send", id_message_send);

                postId.post(URL_UPDATE, id, new AsyncHttpResponseHandler() {
                    @Override
                    public void onStart() {
                        // TODO Auto-generated method stub
                        super.onStart();
                    }

                    @Override
                    public void onSuccess(int arg0, String response) {
                        Log.i("INFO", "Response Server " + response);
                        super.onSuccess(arg0, response);
                    }

                });
                break;
            case Activity.RESULT_CANCELED:
                Log.i("INFO", "SMS NOT Deliver");
                break;
            }

        }
    }, new IntentFilter(DELIVERED));

}
1

There are 1 best solutions below

2
On

You need to make sure that your PendingIntents are all unique. Since you are always providing the same ACTION in the Intent you pass to createBroadcast(), you are always getting the same PendingIntent back.

Try setting the requestCode parameter to a unique value every time you call createBroadcast(), like this (assuming that your id parameter is unique):

deliverPI = PendingIntent.getBroadcast(getBaseContext(), id, deliverIntent, 0);
sentPI = PendingIntent.getBroadcast(getBaseContext(), id, sendinten, 0);