mboy mboy - 19 days ago 7
Android Question

Getting unwanted multiple intents on broadcast receiver

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);
}
}

Answer

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);
Comments