Differentiating delivery reports of two separate SMS messages

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

except that I think it uses it in action, and I use it in a service where getIntent () is not successful.

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; } } 
+4
android sms report
Oct 08
source share
1 answer

You add an additional message number to the intent as follows:

 delivered.putExtra("MsgNum", serialnum); 

and you try to extract it like this:

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

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

That is why you should always use constants for such things. This prevents you from spending hours trying to find errors caused by typographical errors.

Try the following:

 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 PendingIntent based on OP comment

OP wrote in a comment:

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

Your problem is how PendingIntent works. The system manages the PendingIntent s pool. 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 forces the system to look for a PendingIntent that matches the parameters you passed (in this case, your Intent ). However, the matching algorithm that PendingIntent only uses compares specific Intent fields to determine if it is the one you are looking for. In particular, it does not compare additional features . So this means that after creating the first PendingIntent calling PendingIntent.getBroadcast() will always return the same PendingIntent from the pool (rather than creating a new one that you want).

To make the call PendingIntent.getBroadcast() create a new PendingIntent every time you call it, try to make the parameters that you pass to the unique call (for example: making ACTION in Intent unique), in addition, since each of these PendingIntent will be used only after setting FLAG_ONE_SHOT when receiving a PendingIntent as follows:

 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 ACTION will be different for every call to PendingIntent.getBroadcast() , this should solve your problem.

EDIT2: add an alternative method for registering broadcast receivers based on discussion in comments

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

 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 recipient in the manifest:

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

In your code that sends SMS, do the following:

 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); 
+11
Oct 10
source share



All Articles