Search code examples
androidandroid-serviceandroid-broadcast

Send sms pending intent extras get overide


Hi I'm sending messsage from my app through SmsManager in my service and register broadcast receivers for sms sent and delivered where I want to update message status as it is reached or not. As per now I'm able to send messages by putting extras with row id in Pending Intent with flag PendingIntent.FLAG_UPDATE_CURRENT. But once messages is send I'm receiving broadcast events with extras of last row only and I'm not able to retrieve extras of other rows. I'm using PendingIntent.FLAG_UPDATE_CURRENT that is why extras are getting override and I have checked other flags also but of no use. How to get extras of other rows also without being override? and one more thing I am sending sms in service is this approach proper? Any suggestions will be
of great help.

  String mSelectionClause = MyContentprovider.STATUS+ " = ?";
                 String[] mSelectionArgs = {"fail"};


                    Uri students = MyContentprovider.CONTENT_URI_SENDMSG;
                    Cursor c = getContentResolver().query(students, null, mSelectionClause, mSelectionArgs, "userphno");
                    if (c.moveToFirst()) {
                       do{



                               String SENT = "SMS_SENT";
                                String DELIVERED = "SMS_DELIVERED";

                                Intent SENTINTENT=new Intent(SENT);
                                SENTINTENT.putExtra("id",c.getString(c.getColumnIndex( MyContentprovider._ID)));
                                SENTINTENT.putExtra("userno", c.getString(c.getColumnIndex( MyContentprovider.USER_PHNO)));
                            if(c.isLast()){

                                SENTINTENT.putExtra("lastrow", "true");

                            }


                                Intent DELIVEREDINTENT=new Intent(DELIVERED);
                                DELIVEREDINTENT.putExtra("id",c.getString(c.getColumnIndex( MyContentprovider._ID)));
                                DELIVEREDINTENT.putExtra("userno", c.getString(c.getColumnIndex( MyContentprovider.USER_PHNO)));

                                  PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
                                          SENTINTENT, PendingIntent.FLAG_UPDATE_CURRENT);


                                  PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                                          DELIVEREDINTENT, PendingIntent.FLAG_UPDATE_CURRENT);

                                  String msg= c.getString(c.getColumnIndex( MyContentprovider.MESSAGE));

                       SmsManager sms = SmsManager.getDefault();
                                                      sms.sendTextMessage( c.getString(c.getColumnIndex( MyContentprovider.USER_PHNO)), null, msg, sentPI, deliveredPI);

and in broadcast receiver and I am storing the ids of row nothing much code there


Solution

  • The solution I found is to pass a unique int for the requestCode (the second argument) in:

    PendingIntent.getBroadcast(this, 0, SENTINTENT, PendingIntent.FLAG_UPDATE_CURRENT)
    

    Then it creates a new PendingIntent for each.