Reputation: 755
Can anyone give some hints why this broadcast receiver receives multiple intents? Example I sent only one sms but broadcast receiver received it multiple times.
I already checked on this threads but no luck Getting multiple broadcasts from intents? BroadCastReceiver for Multiple sent Messages?
public void sendMultipart(String msgbody, String msg_receipients, Intent intent)
{
Intent intentExtra = new Intent("SMS_SENT");
intentExtra.putExtra("phoneNumber", msg_receipients);
intentExtra.putExtra("msgbody", msgbody);
PendingIntent sentPI =
PendingIntent.getBroadcast(MyApplication.getAppContext(),
smsID++, intentExtra, PendingIntent.FLAG_CANCEL_CURRENT);
BroadcastReceiver smsStatusReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String phoneNumber = intent.getStringExtra("phoneNumber");
String message = intent.getStringExtra("message");
int resultCode = getResultCode();
switch (resultCode)
{
case Activity.RESULT_OK:
Logger.logtoFile(tag,"RESULT_OK "+phoneNumber,1);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Logger.logtoFile(tag,"RESULT_ERROR_GENERIC_FAILURE "+phoneNumber,1);
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Logger.logtoFile(tag,"RESULT_ERROR_NO_SERVICE "+phoneNumber,1);
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Logger.logtoFile(tag,"RESULT_ERROR_RADIO_OFF "+phoneNumber,1);
break;
}
}
};
MyApplication.getAppContext().registerReceiver(smsStatusReceiver, new IntentFilter("SMS_SENT"));
try
{
SmsManager sms = SmsManager.getDefault();
ArrayList<String> messages = sms.divideMessage(msgbody); //Divide msg into chunk
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
sentIntents.add(sentPI);
for (int j = 0; j < messages.size(); j++) {
sentIntents.add(PendingIntent.getBroadcast(MyApplication.getAppContext(),0,intent, 0));
sentIntents.add(null);
}
sms.sendMultipartTextMessage(msg_receipients, null, messages, sentIntents, null);
} catch(Exception l) {
Logger.logtoFile(tag, l.toString(), 2);
}
}
Upvotes: 3
Views: 2082
Reputation: 21
I've had a similar problem receiving multiple unwanted broadcasts.
In my case I registered a BroadcastReceiver in my child activity's onCreate(), but never unregistered it when activity closed. So, when my parent activity launched the child activity again, a new registration occured, and instead of 1 broadcast I got 2,3 and so on.
The problem was solved by putting registerReceiver into onResume() and unregisterReceiver into onPause.
Also if you use LocalBroadcastManager.registerReceiver, don't forget to use LocalBroadcastManager.unregisterReceiver, not just unregisterReceiver, or you will get a java.lang.IllegalArgumentException: Receiver not registered
Upvotes: 2
Reputation: 755
After I added a line to unregister my broadcastreceiver the result was just the same until I upgraded my emulator to Android 4.1.2. It appears there is a bug on Android 2.3.3. This problem is now fixed..
context.unregisterReceiver(this);
Upvotes: 1