Reputation: 23
BroadcastReceiver life cycle and operation is not clear to me, I already looked at android help library, but still it’s not clear, consider the following example:
private void sendSMS(final MessageInfo MI) {
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:
Confirm(MI);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "שליחה נכשלה",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "שליחה נכשלה, אין רשת",
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(), "שליחה נכשלה, מצב טיסה",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(MI.Message());
int numParts = parts.size();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
for (int i = 0; i < numParts; i++) {
sentIntents.add(sentPI);
}
sms.sendMultipartTextMessage(MI.Phone(), null, parts, sentIntents, null);
}
}
What if I send a large number of messages, will it create a BroadcastReceiver for each message? and when the sent confirmation is returned, is it return asynchronously or synchronously? does the BroadcastReceiver unregister automatically, or does it need to be done manually?
I’ll appreciate any help, and I hope I am clear.
BTW the Confirm function send confirmation to the server (asynchronously) and the MI class is MessageInfo(phone,body,ID)
Upvotes: 1
Views: 847
Reputation: 1006819
What if I send a large number of messages, will it create a BroadcastReceiver for each message?
Yes. This is not a good idea. Please register it ONCE.
and when the sent confirmation is returned, is it return asynchronously or synchronously?
Asynchronously.
does the BroadcastReceiver unregister automatically
No.
does it need to be done manually?
Yes, via unregisterReceiver()
.
Upvotes: 3