BroadcastReceiver Problem

MarkBoettcherMarkBoettcher USMember ✭✭
edited November 2012 in Xamarin.Android

I am porting a large Android project to Mono and the latest problem of many has been beating me up for a couple of hours now. I have created java bindings for the AndroidNGN library. Everything works fine except for the OnReceive. The Java version worked great, but I can not get the C# version to behave. Here is my part of my subclassed BroadcastReceiver:

public class SipMessageBroadCastRecv : Android.Content.BroadcastReceiver
    public override void OnReceive(Context context, Intent intent)
        String action = intent.Action;

        if (NgnMessagingEventArgs.ActionMessagingEvent.Equals(action))
            bool b = intent.HasExtra(NgnEventArgs.ExtraEmbedded);
            NgnMessagingEventArgs args = (NgnMessagingEventArgs)intent.GetParcelableExtra(NgnEventArgs.ExtraEmbedded);

It gets setup like this:

IntentFilter msgIntentFilter = new IntentFilter();
msgIntentFilter.AddAction( NgnMessagingEventArgs.ActionMessagingEvent);
mSipMessageBroadCastRecv = new SipMessageBroadCastRecv();

RegisterReceiver(mSipMessageBroadCastRecv, msgIntentFilter);

This all works fine, no errors.

OnReceive() is getting called properly, NgnMessagingEventArgs.ActionMessagingEvent.Equals(action) is true and b is set to true. However it complains that I can't do the cast to NgnMessagingEventArgs. Works just fine in my original Droid project. Tried:

var foo = intent.GetParcelableExtra(NgnEventArgs.ExtraEmbedded);

and foo gets set, but I can't cast foo to NgnMessagingEventArgs either.

I have a couple of other JARs like ActionBarSherlock and OSMDroid and some .so native libraries and JARs calling native libraries, so I think I have a pretty good handle on this stuff, but obviously I am missing something.

Sorry for the horrid formatting, but I don't have the time just now to figure out this editor.



  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    You're hitting Bug 7459. The workaround is to use the .JavaCast<T>() extension method:

    var args = intent.GetParcelableExtra(NgnEventArgs.ExtraEmbedded)
  • MarkBoettcherMarkBoettcher USMember ✭✭

    Thanks for the quick response. I had it up and running about 5 minutes after you sent that yesterday.

Sign In or Register to comment.