3

I've been searching for a way to send a local message within my app and found a tutorial from the Xamarin website on Broadcast Receivers here, more specifically at the bottom of the web page concerning LocalBroadcastManager. I followed the tutorial and read the page a few times but my BroadcastReceiver class still isn't receiving anything when I send a message. I've hit a lot of the questions concerning LocalBroadcastManager for java, but can't seem to figure out what's missing for C#.

This is the code that's triggering a sent message:

Intent intent = new Intent("dirty");
intent.PutExtra("dirtyAppCount", dirtyAppCount);
LocalBroadcastManager.GetInstance(Context).SendBroadcast(intent);

Here's where I'm registering my receiver in OnResume():

_dirtyMessageReceiver = new DirtyBroadcastReceiver();
RegisterReceiver(_dirtyMessageReceiver, new IntentFilter("dirty"));

Unregistering receiver in OnPause():

UnregisterReceiver(_dirtyMessageReceiver);

And here's my receiver class:

[BroadcastReceiver(Enabled = true, Exported = false)]
public class DirtyBroadcastReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        int dirtyAppCount = intent.GetIntExtra("dirtyAppCount", 0);
        OnMessageReceived?.Invoke(this, new MessageArgs(dirtyAppCount));
    }
}

2 Answers2

3

There are two issues with this code. First, you should use be registering the Receiver with the LocalBroadcastManager:

 _dirtyMessageReceiver = new DirtyBroadcastReceiver();
RegisterReceiver(_dirtyMessageReceiver, new IntentFilter("dirty"));

Should be

 _dirtyMessageReceiver = new DirtyBroadcastReceiver();
 LocalBroadcastManager.GetInstance(this).RegisterReceiver(_dirtyMessageReceiver, new IntentFilter("dirty"));

Secondly, the Unregistering of the Receiver should be one against the LocalBroadcastManager as well:

UnregisterReceiver(_dirtyMessageReceiver);

becomes

LocalBroadcastManager.GetInstance(this).UnregisterReceiver(_dirtyMessageReceiver);
  • Aw... I knew it was something simple :/ I assumed that registering/unregistering broadcast receivers the way I did would catch everything. Guess not. Thanks! – Brooks Lindsey Jul 19 '17 at 19:46
0

You need to add a broadcast receiver for these.For example, Set Android.Content.Intent to ActionTimeTick so that android os will broadcast message(an android intent) whenever time is changed.

[BroadcastReceiver(Enabled = true)]
    [IntentFilter(new[] { Android.Content.Intent.ActionTimeTick })]
    public class GridStartBroadcastReceiver : BroadcastReceiver
    {
        public static readonly string GRID_STARTED = "GRID_STARTED";
        public override void OnReceive(Context context, Intent intent)
        {
           if (intent.Action == GRID_STARTED)
            {
         //your logic
            }
        }
    }

you need to register the broadcast receiver first. Add these code to oncreate method to register broadcast receiver.

IntentFilter filter = new IntentFilter(GridStartBroadcastReceiver.GRID_STARTED);
            filter.AddCategory(Intent.CategoryDefault);
            _receiver = new GridStartBroadcastReceiver();
            RegisterReceiver(_receiver, filter);

Next send the broadcast to the broadcast receiver.

//calling
                    Intent BroadcastIntent = new Intent(this, typeof(MainActivity.GridStartBroadcastReceiver));
                    BroadcastIntent.SetAction(MainActivity.GridStartBroadcastReceiver.GRID_STARTED);
                    BroadcastIntent.AddCategory(Intent.CategoryDefault);
                    SendBroadcast(BroadcastIntent);
yash darak
  • 368
  • 4
  • 17