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;
}
}
You add the message number extra to the Intent like this:
and you try to extract it like this:
In
putExtra()you have uppercase “N”, ingetStringExtra()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:
then use:
and:
EDIT: Add something about generating different
PendingIntents based on OP’s commentOP wrote in a comment:
Your problem is the way
PendingIntentworks. The system manages a pool ofPendingIntents. When your code does:This causes the system to search for a
PendingIntentthat matches the parameters you’ve passed in (in this case, yourIntent). However, the matching algorithm thatPendingIntentuses only compares certain fields of theIntentto 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 firstPendingIntent, the call toPendingIntent.getBroadcast()will always return the samePendingIntentfrom the pool (and not create a new one, which is what you want).In order to make the call to
PendingIntent.getBroadcast()create a newPendingIntentevery time you call it, try making the parameters you pass to the call unique (for example: by making the ACTION in theIntentunique). Also, since each of thesePendingIntents will only be used once you should set the FLAG_ONE_SHOT when obtaining thePendingIntentlike this: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:
Declare the receiver in your manifest:
In your code that sends the SMS, do this: