Question

I'm sending a sticky broadcast from my service, and I receive it in one of my activities. I use the following code to send the broadcast:

Intent i = new Intent();
i.setAction("presence");
i.putExtra("name",xmlContactName);
i.putExtra("fullname", xmlContactName.split("@")[0]);
i.putExtra("presence", presence);

sendStickyBroadcast(i);

I use this code to receive the broadcast in my activity:

receiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if(action.equalsIgnoreCase("presence")){
      Contact c = new Contact();
      Bundle contact = intent.getExtras();
      c.name = contact.getString("name");
      c.fullName = contact.getString("fullname");
      c.presence = contact.getString("presence");

      removeStickyBroadcast(intent);
    }
  };
}

According to several posts I found this is all I need to do, and as far as I know this intent is not sent to the activity again while my app is still running. However, when I close the app and run it again, I get the broadcast in my activity even without sending it from my service. It seems as though the broadcast is still lingering somewhere and it is received again after restarting the app. So far, the only thing I found to get rid of it is to restart the phone. I also unregister the receiver in my activity's onDestroy().
Do I have to do anything to completely remove the broadcast, or is it something I have to learn to live with?

Was it helpful?

Solution

Sticky broadcasts stick around even after your app is gone. The only reliable way to get rid of them is through phone restart. You can't manually make it go away yourself. This is the intended behavior of a sticky broadcast. It should stick around so that any app that registers itself to receive this broadcast should receive the latest cached version of the broadcast even if the originating app is dead and gone. If you want something that doesn't have this behavior then just use a regular broadcast.

Further reading:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top