Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broadcast receiver register on android 14.0/34+ #18520

Merged
merged 4 commits into from
Nov 14, 2023

Conversation

Redth
Copy link
Member

@Redth Redth commented Nov 3, 2023

This adds a new helper method for registering broadcast receivers, which takes a bool for exporting the service or not, and on API 34 and higher, it calls the newer overload which takes the ReceiverFlags and passes the corresponding NotExported or Exported value to the bool parameter of the util method.

This allows us to call the right overload if we are on API 34 (Android 14.0) or newer where the declaration is required.

Also some older GetLocale()/SetLocale() methods are removed which were unused.

Fixes #17861

@Redth
Copy link
Member Author

Redth commented Nov 3, 2023

I haven't been able to test on an Android 14.0 (API 34) device, and the documentation is not the clearest to me:

If this receiver is listening for broadcasts sent from the system or from other apps—even other apps that you own—use the RECEIVER_EXPORTED flag. If instead this receiver is listening only for broadcasts sent by your app, use the RECEIVER_NOT_EXPORTED flag.

We are listening for system events, so it sounds like we should use RECEIVER_EXPORTED however it seems to work just fine with RECEIVER_NOT_EXPORTED on the simulator.

@Eilon Eilon added the area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info label Nov 9, 2023
@Redth Redth marked this pull request as ready for review November 13, 2023 18:17
@Redth Redth requested a review from a team as a code owner November 13, 2023 18:17
PureWeen
PureWeen previously approved these changes Nov 13, 2023
Copy link
Member

@PureWeen PureWeen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the connectivity sample in essentials and it no longer crashes with these changes

This adds a new helper method for registering broadcast receivers, which takes a bool for exporting the service or not, and on API 34 and higher, it calls the newer overload which takes the `ReceiverFlags` and passes the corresponding `NotExported` or `Exported` value to the bool parameter of the util method.

This allows us to call the right overload if we are on API 34 (Android 14.0) or newer where the declaration is required.

Also some older GetLocale()/SetLocale() methods are removed which were unused.
Copy link
Member

@mattleibow mattleibow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs tests, will add.

@mattleibow mattleibow force-pushed the dev/redth/android14-broadcastreceivers branch from afd5840 to 17d928a Compare November 13, 2023 22:00
@mattleibow mattleibow dismissed their stale review November 13, 2023 22:00

added tests

@mattleibow
Copy link
Member

I tested on a Pixel 6 with Android 14 / API 34 and a Samsung A53 5G with Android 13 / API 33. I could reproduce the crash for Connectivity, and this PR fixed it. I did not observe crashes for Battery, and this PR does not seem to change anything.

I added some tests that confirmed my observations before & after the PR.

@PureWeen PureWeen enabled auto-merge (squash) November 13, 2023 22:09
@mattleibow mattleibow added this to the .NET 8 SR1 milestone Nov 13, 2023
@mattleibow mattleibow removed this from the .NET 8 SR1 milestone Nov 13, 2023
@mattleibow
Copy link
Member

Once this is merged, it should appear on the nightly feeds.

@PureWeen PureWeen merged commit 6ec214f into main Nov 14, 2023
48 checks passed
@PureWeen PureWeen deleted the dev/redth/android14-broadcastreceivers branch November 14, 2023 09:46
@Alex-Dobrynin
Copy link
Contributor

When this fix will be released? Today I've downloaded new VS with .net 8 and other updates and got this crash

@github-actions github-actions bot locked and limited conversation to collaborators Dec 16, 2023
@samhouts samhouts added the fixed-in-8.0.6 Look for this fix in 8.0.6 SR1! label Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info fixed-in-8.0.6 Look for this fix in 8.0.6 SR1! platform/android 🤖
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding a Connectivity.ConnectivityChanged handler throw an exception on Android 14
6 participants