6

In a service, when sending smses in a loop with a broadcast receiver listening for delivery reports, how to differentiate the delivery report of each sms sent? This is similar to : How to get Delivery Report of each SMS sent in loop android?

except that I think he's using it in an activity and I'm using it in a service where getIntent() is of no avail.

Edit 2: posting my code

public class CheckServer extends Service
{
  public String snumber[] = new String[10];

  public JSONArray array;

  public int onStartCommand(Intent intent,int flags, int startid)
  {
    // Do useful things.

  ServiceAction SA = new ServiceAction();
  SA.execute();

  try 
  {
      SA.get();
  } 
  catch (InterruptedException e) 
  {
      e.printStackTrace();
  } 
  catch (ExecutionException e) 
  {
      e.printStackTrace();
  }


  new startSending().execute();

  scheduleNextUpdate();

   return START_STICKY;
}

public class startSending extends AsyncTask<Void,Void,Void>
{
    @Override
    protected Void doInBackground(Void... params) 
    {
        String no,message;

          try 
            {
                for (int i = 0; i < array.length(); i++) 
                {
                      JSONObject row;
                      row = array.getJSONObject(i);
                      snumber[i] = row.getString("sno");
                      no = row.getString("no");
                      message = row.getString("message");
                      sendSMS(no,message,snumber[i]);
                }
            } 
            catch (IllegalStateException e) 
            {
                e.printStackTrace();
            } 
            catch (JSONException e) 
            {
                e.printStackTrace();
            }

        return null;
    }
}

private void scheduleNextUpdate()
{
  Intent intent = new Intent(this, this.getClass());
  PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

// The update frequency should often be user configurable.  This is not.

long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis + 1 * DateUtils.MINUTE_IN_MILLIS;
Time nextUpdateTime = new Time();
nextUpdateTime.set(nextUpdateTimeMillis);

  AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
}

public class ServiceAction extends AsyncTask<Void,Void,Void>
{
@Override
protected Void doInBackground(Void... arg0) 
{
      HttpResponse response = null;
      HttpClient client = new DefaultHttpClient();
      HttpGet request = new HttpGet();
      try 
      {
        request.setURI(new URI("http://www.somesite.com/sms/getsms"));
        response = client.execute(request);
        String result = convertStreamToString(response.getEntity().getContent());
        array = new JSONArray(result);
      } 
      catch (URISyntaxException e) 
      {
        e.printStackTrace();
      } 
      catch (ClientProtocolException e) 
      {
        e.printStackTrace();
      } 
      catch (IOException e) 
      {
        e.printStackTrace();
      } 
      catch (JSONException e) 
      {
        e.printStackTrace();
      }


    return null;
}
}

public static String convertStreamToString(InputStream inputStream) throws IOException
{
  if (inputStream != null)
  {
      Writer writer = new StringWriter();

      char[] buffer = new char[1024];
      try
      {
          Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"),1024);
          int n;
          while ((n = reader.read(buffer)) != -1)
          {
              writer.write(buffer, 0, n);
          }
      }
      finally
      {
          inputStream.close();
      }
      return writer.toString();
  }
  else
  {
      return "";
  }
}


public void sendSMS(String number,String message,String serialnum) 
{   
  String SENT = "SMS_SENT";

  PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
      new Intent(SENT), 0);

  //---when the SMS has been sent---
  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();
                  break;
              case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                  Toast.makeText(getBaseContext(), "Generic failure", 
                          Toast.LENGTH_SHORT).show();
                  break;
              case SmsManager.RESULT_ERROR_NO_SERVICE:
                  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:
                  Toast.makeText(getBaseContext(), "Radio off", 
                          Toast.LENGTH_SHORT).show();
                  break;
          }

          unregisterReceiver(this);
      }
  }, new IntentFilter(SENT));

  String DELIVERED = "SMS_DELIVERED";

  Intent delivered = new Intent(DELIVERED);
  delivered.putExtra("MsgNum", serialnum);
  PendingIntent deliveredPI = PendingIntent.getBroadcast(this, Integer.parseInt(serialnum), delivered, 0);

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver()
        {   
            @Override
           public void onReceive(Context context, Intent intent)
           {
                       switch (getResultCode())
                       {
                           case Activity.RESULT_OK:

                               Toast.makeText(getBaseContext(), "SMS delivered",Toast.LENGTH_SHORT).show();

                               updateSMSStatus USS =  new updateSMSStatus();

                               USS.execute(intent.getStringExtra("Msgnum"));

                               break;

                           case Activity.RESULT_CANCELED:

                               Toast.makeText(getBaseContext(), "SMS not delivered",Toast.LENGTH_SHORT).show();

                               break;                        
                       }

                     unregisterReceiver(this);
            }

        },
        new IntentFilter(DELIVERED)); 

    ContentValues values = new ContentValues();
    values.put("address", number);
    values.put("body", message);
    getContentResolver().insert(Uri.parse("content://sms/sent"), values);

    SmsManager smsMngr = SmsManager.getDefault();
    smsMngr.sendTextMessage(number, null, message, sentPI, deliveredPI);

}

public class updateSMSStatus extends AsyncTask<String,Void,Void>
{
    @Override
    protected Void doInBackground(String... params) {

          HttpResponse response = null;
          HttpClient client = new DefaultHttpClient();
          HttpGet request = new HttpGet();
          try 
          {
            Log.i("SMS APP", "MyClass.getView() — Serial Number = " + params[0]);
            request.setURI(new URI("http://www.somesite.com/sms/updatesmsstatus?uname=someone&sno="+params[0]));
            response = client.execute(request);
            String result = convertStreamToString(response.getEntity().getContent());
            Log.i("SMS APP","Update SMS Status is :"+result);
          } 
          catch (URISyntaxException e)
          {
            e.printStackTrace();
          } 
          catch (ClientProtocolException e) 
          {
            e.printStackTrace();
          } 
          catch (IOException e) 
          {
            e.printStackTrace();
          }

        return null;
    }
}

  @Override
  public IBinder onBind(Intent intent) 
  {
     return null;
  }

}
Community
  • 1
  • 1
somedev
  • 468
  • 8
  • 20
  • 1
    The solution described in http://stackoverflow.com/questions/10975792/how-to-get-delivery-report-of-each-sms-sent-in-loop-android will work for you too. You have a `BroadcastReceiver` listening for the delivery reports. The extras are delivered in the `Intent` that the `BroadcastReceiver` gets. What exactly is your problem? – David Wasser Oct 08 '12 at 13:36
  • @David Wasser,i have my send sms function in a service instead of an activity which i can dynamically start/stop, the value I get on intent.getStringExtra("Key") in the onReceive() function is null, that is the problem – somedev Oct 10 '12 at 06:03
  • Post the code in your receiver and also the code you use to send the SMS (including the PendingIntent stuff). – David Wasser Oct 10 '12 at 07:24
  • @DavidWasser : posted my code, im running it as a background service – somedev Oct 10 '12 at 10:09

1 Answers1

12

You add the message number extra to the Intent like this:

delivered.putExtra("MsgNum", serialnum);

and you try to extract it like this:

USS.execute(intent.getStringExtra("Msgnum"));

In putExtra() you have uppercase "N", in getStringExtra() you use lowercase "n".

This is why you should always use constants for stuff like this. It prevents you spending hours trying to find errors caused by typographical errors.

Try this:

public static final String EXTRA_MSGNUM = "MsgNum";

then use:

delivered.putExtra(EXTRA_MSGNUM, serialnum);

and:

USS.execute(intent.getStringExtra(EXTRA_MSGNUM));

EDIT: Add something about generating different PendingIntents based on OP's comment

OP wrote in a comment:

My bad for the typo, felt like a sheep due to that, i tested it, it does not give a null value now, instead, it gives me the serial number of the first message sent in the loop for all messages, if i send 17 20 24 21 25 27, it gives me only 17 for all the delivery reports

Your problem is the way PendingIntent works. The system manages a pool of PendingIntents. When your code does:

String DELIVERED = "SMS_DELIVERED";
Intent delivered = new Intent(DELIVERED);
delivered.putExtra("MsgNum", serialnum);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this,
                    Integer.parseInt(serialnum), delivered, 0);

This causes the system to search for a PendingIntent that matches the parameters you've passed in (in this case, your Intent). However, the matching algorithm that PendingIntent uses only compares certain fields of the Intent to determine if it is the one that you are looking for. In particular, it does not compare extras. So this means after you've created the first PendingIntent, the call to PendingIntent.getBroadcast() will always return the same PendingIntent from the pool (and not create a new one, which is what you want).

In order to make the call to PendingIntent.getBroadcast() create a new PendingIntent every time you call it, try making the parameters you pass to the call unique (for example: by making the ACTION in the Intent unique). Also, since each of these PendingIntents will only be used once you should set the FLAG_ONE_SHOT when obtaining the PendingIntent like this:

String DELIVERED = "SMS_DELIVERED" + serialnum; // Unique ACTION every time
Intent delivered = new Intent(DELIVERED);
delivered.putExtra("MsgNum", serialnum);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this,
                    Integer.parseInt(serialnum), delivered,
                    PendingIntent.FLAG_ONE_SHOT);

Since the ACTION will be different for each call to PendingIntent.getBroadcast(), this should solve your problem.

EDIT2: Add alternative method of registering broadcast receivers based on discussion in comments

If you create a class that extends BroadcastReceiver, you can add that to the manifest and then you don't need to explicitly register the broadcast receiver at all. Something like this:

public class MessageStatusReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // This is called when the status of your SMS changes (delivery or send status)
        // .. put your code here ..
   }
}

Declare the receiver in your manifest:

<receiver android:name=".MessageStatusReceiver" />

In your code that sends the SMS, do this:

String DELIVERED = "SMS_DELIVERED" + serialnum; // Unique ACTION every time
Intent delivered = new Intent(context, MessageStatusReceiver.class);
delivered.setAction(DELIVERED ); // Set action to ensure unique PendingIntent
delivered.putExtra("MsgNum", serialnum);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this,
                    Integer.parseInt(serialnum), delivered,
                    PendingIntent.FLAG_ONE_SHOT);
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • My bad for the typo, felt like a sheep due to that, i tested it, it does not give a null value now, instead, it gives me the serial number of the first message sent in the loop for all messages, if i send 17 20 24 21 25 27, it gives me only 17 for all the delivery reports – somedev Oct 11 '12 at 09:53
  • Ah...that's because of the way `PendingIntent` works. I'll add something to my answer about this. – David Wasser Oct 11 '12 at 10:24
  • That solves the problem, works as expected and thanks for the explanation of Pending Intent and the way it works, learnt a lot from you Sir :) – somedev Oct 12 '12 at 07:30
  • and this will answer many questions, a lot of people asked the same question, it definitely will answer that – somedev Oct 12 '12 at 07:32
  • That means one need to register as many broadcast receivers as SMS, because each one has a unique Intent. – Gaurav Agarwal Feb 03 '13 at 23:06
  • @codingcrow You can do this in a slightly different way to avoid having to register multiple receivers. If you explicitly specify the component name in the `Intent` (ie: with `Intent delivered = new Intent(this, MyReceiver.class); delivered.setAction(DELIVERED);`, then you don't have to dynamically register the broadcast receiver at all. You just define the receiver in the manifest. – David Wasser Feb 04 '13 at 08:45
  • Added code for the receiver to you answer, is that what you meant? – Gaurav Agarwal Feb 04 '13 at 16:29
  • @codingcrow Yes, more or less. But I'm not accepting that edit because it muddies the waters. OP asked a question which I answered and OP accepted. The answer is not wrong, so I don't see any reason to edit it. There are often many ways to solve a problem. OP was obviously happy with this answer. If you think it necessary to post your code as a solution, then I suggest you create another answer. – David Wasser Feb 04 '13 at 16:51
  • @David Wasser Is there another way to avoid registering multiple broadcast receivers?If so, then how? – somedev Mar 05 '13 at 08:53
  • @coding crow, can you post that receiver code in a new answer? – somedev Mar 05 '13 at 08:55
  • Added code to show an alternative method of dealing with the broadcast receivers. This method doesn't require registering any broadcast receivers (there is only one and it is configured in the manifest) – David Wasser Mar 05 '13 at 10:39
  • @DavidWasser Thank You, I suppose the same can also be done for send intent broadcast receiver? – somedev Mar 16 '13 at 05:07
  • Yes, the same can be used for other types of `BroadcastReceiver`s – David Wasser Mar 16 '13 at 15:16