Skip to content

Argument type of Context.RegisterReceiver() is different from native API #7503

Description

@aqua-ix

Android application type

Classic Xamarin.Android (MonoAndroid12.0, etc.)

Affected platform version

MonoAndroid 13.0, Xamarin.Android 13.1.0.1 , VS Mac 17.4

Description

I am currently updating the version of the target SDK for my Xamarin.Android project to version 33.

One of the new features of Android 13 is "Safer exporting of context-registered receivers".
https://developer.android.com/about/versions/13/features#runtime-receivers

To make my app compatible with this, I was going to change the RegisterReceiver call point as follows:

- Context.RegisterReceiver(receiver, filter);
+ Context.RegisterReceiver(receiver, filter, ReceiverFlags.Exported);

This implementation conforms to the specification of the following method: https://developer.android.com/reference/android/content/Context#registerReceiver(android.content.BroadcastReceiver,%20android.content.IntentFilter,%20java.lang.String,%20android.os.Handler)

However, the above code failed to compile.
After investigating the cause, I found that the third argument of MonoAndroid's RegisterReceiver() overload is not of type Int, but ActivityFlags. This differs from the native API specification.

Please let me know why this is happening and if MonoAndroid plans to fix it.

Steps to Reproduce

  1. Create new Xamarin.Android project that targeting MonoAndroid 13.0
  2. Call the following overloads of RegisterReceiver at any position:
    RegisterReceiver(BroadcastReceiver, IntentFilter, Int)

Did you find any workaround?

I have confirmed that ReceiverFlags can be used with ContextCompat API and will consider replacing it with ContextCompat in the near future if this issue is not resolved.
https://developer.android.com/reference/androidx/core/content/ContextCompat#registerReceiver(android.content.Context,android.content.BroadcastReceiver,android.content.IntentFilter,int)

Relevant log output

No response

Activity

  1. added
    Area: Mono.AndroidIssues with the Android API binding (Mono.Android.dll).
    needs-triageIssues that need to be assigned.
    on Oct 28, 2022
  2. jpobst commented on Oct 28, 2022

    @jpobst
    Contributor

    It looks like this method has been enumified incorrectly as ActivityFlags instead of ReceiverFlags.

    All enums in C# are really int behind the scenes, so you can cast them as needed to workaround this.

    Context.RegisterReceiver(receiver, filter, (ActivityFlags)ReceiverFlags.Exported);
  3. added this to the .NET 8 milestone on Oct 28, 2022
  4. aqua-ix commented on Oct 28, 2022

    @aqua-ix
    Author

    Thank you for your response.
    I consider using type casting until this issue is resolved.

  5. locked and limited conversation to collaborators on Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area: Mono.AndroidIssues with the Android API binding (Mono.Android.dll).

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions