Closed daniel-c closed 1 year ago
There is a breaking change for Android API 34 that requires setting the RecieverFlags. I believe if we update
https://github.com/dotnet/maui/blob/main/src/Essentials/src/Connectivity/Connectivity.android.cs#L48
to this new API,
Application.Context.RegisterReceiver(conectivityReceiver, filter, ReceiverFlags.NotExported);
it would work again... the problem being, it's only in the Android 34 dlls (In that, it's in the preprocessor defs to only be compiled for Android 34, and as far as I can tell can't be referenced on early versions. I'm not sure if MAUI ships multiple editions for essentials that target specific Android versions like that.
@jonpryor @jpobst do ya'll know how to access this API in Essentials? Or did I misunderstand the problem?
Context: https://github.com/xamarin/xamarin-android/commit/d9e440710574e3d261faa4514eb1a46091bca9b9
This new API will only be available for projects targeting .NET 8+. If you want to be able to call this API for earlier projects you will need to call the incorrectly enumified version that has existed for many years:
You can cast the enums as needed:
Application.Content.RegisteredReceiver(conectivityReceiver, filter, (ActivityFlags)ReceiverFlags.NotExported);
Verified this on Visual Studio Enterprise 17.8.0 Preview 2.0(8.0.0-rc.1.9171). Repro on Android 14.0-API34 with below Project: 17861.zip
verified the issue exists on Xamarin forms as well
We also face this issue. Is there any chance to temporary avoid this exception?
@albertruff You can temporary avoid this exception by setting the Android targetSdkVersion to 33 instead of 34.
@dondestas Thank you. It works
This needs to be updated in other areas too. We need to do an audit of our code base to see where it's implicated and determine the correct values to use.
Got this crash today after update to .net 8
I'm getting this on a project after .net 8 upgrade, specifically during this call:
Connectivity.ConnectivityChanged += OnConnectivityChanged;
EDIT: ah its in nightly builds now... zzzz. i'll wait :P Why is this closed? It's not fixed in .net8 release.
@dylix if maui team counts that it is fixed then it is fixed, don't you understand this?
@dylix closing PRs/issues only when something is released is a pretty hard thing to administer. Whenever something is merged we consider it fixed but it is pending release. Keep you eye on the release notes.
+1 Commenting so I can keep track of when this gets released
+1 Commenting so I can keep track of when this gets released
Commenting your comment for the same reason
+1
+1 Commenting so I can keep track of when this gets released
Commenting your comment for the same reason
+1
+1
+1
+1
Are you able to provide an update on the timing of when this fix will get released?
Hello :) Getting same error here .Net8/Android 34 with Connectivity. Always a pleasure joining other guys on the side of the road.
@jfversluis Hello Guys. What is the final status of this issue? Is it resolved now? Yesterday I've run mobile app with the newest version of CommunityToolkit.Maui
but the issue still occurs.
This issue has been resolved and a fix is merged but is yet to be released. I expect it to be part of the upcoming service release.
Thank you for your answer. So be patient and wait for new release.
@albertruff You can temporary avoid this exception by setting the Android targetSdkVersion to 33 instead of 34.
How?
<TargetFrameworks>net8.0-android33.0</TargetFrameworks>
is not valid target.
Explicitly in the AndroidManifest under the Platform folder
Still get the exception on Connectivity.ConnectivityChanged in the newest pre-release from 3days ago, not sure if it's my fault or if the pre-release didn't include the fix
Hi @jfversluis Is the version out? still have the same issue
I don't think this has been released yet. If you want to be sure, check the nightly feed.
+1
Hello lovely human, thank you for your comment on this issue. Because this issue has been closed for a period of time, please strongly consider opening a new issue linking to this issue instead to ensure better visibility of your comment. Thank you!
So exactly what nightly package is this fix included in?
@dylix I just got around this by using nightly builds. This video got me going. Pioneers get the arrows, settlers get the land. I'm full of arrows.
https://www.youtube.com/watch?v=3EoAGNsYSDk
+1
@dylix I just got around this by using nightly builds. This video got me going. Pioneers get the arrows, settlers get the land. I'm full of arrows. https://www.youtube.com/watch?v=3EoAGNsYSDk
+1 works on newest nightly build
+1
+1
I'm using 8.0.6-nightly.9870. This issue has been resolved but on Android 14 event not trigger when connectivity change. Android 13 and below still works.
Thank for support!
I concur with @TranTrieuLamQuynh. The error is fixed in the nightly builds, but the connection change event is never hit when toggling Wi-Fi on/off. As such, this should be re-opened as it has not been resolved.
Connectivity Change event does not fire in android 34 and also does not fire in iOS
@gabsamples6 We have checked again. Connectivity Change event work in iOS with me.
@mikeluken I used broadcast receiver register on Android and set intent.setPackage(getPackageName()) => the registered receiver will be able to listen for the action.
Note: All, I found the solution for all requireActivity().registerReceiver() calls. They will only listen for the action if the action is sent with the package name of the application. So whenever we send a broadcast, we should also set the package with the intent using intent.setPackage(getPackageName()). Then, the registered receiver will be able to listen for the action.
Reference link: https://developer.android.com/about/versions/14/behavior-changes-14#runtime-receivers-exported
@TranTrieuLamQuynh
Thanks for the comment. However, I do not fully understand your post. I don't have a broadcast receiver to register. In App.xaml.cs, I am just specifying:
Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
In Android 13, this works. In Android 14, Connectivity_ConnectivityChanged
is never hit when toggling on/off Wi-Fi.
Can you please provide a full example of how you got this to work?
It appears as though a fix was already attempted for this:
However, I am running the latest nightly builds and this still does not work.
Note: I also added this to my MainActivity.OnCreate(...):
Intent.SetPackage(AppInfo.PackageName);
It had no effect.
@mikeluken
Particularly for android 14. I don't use
Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
Instead. I created BroadcastReceiver:
[BroadcastReceiver(Enabled = true, Exported = true)]
public class PowerConnectedBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
// Handle when wifi on/off
}
}
Creat in MainActivity:
PowerConnectedBroadcastReceiver _receiver;
IntentFilter _intentFilter;
Then in OnStart() of MainActivity:
if (OperatingSystem.IsAndroidVersionAtLeast(34))
{
Intent.SetPackage(AppInfo.Current.PackageName);
_receiver = new ();
_intentFilter = new ();
_intentFilter.AddAction("android.net.conn.CONNECTIVITY_CHANGE");
_intentFilter.Priority = (int)IntentFilterPriority.HighPriority;
ContextCompat.RegisterReceiver(this, _receiver, _intentFilter, 1);
}
I use this temporary solution until MAUI makes a correction If I have anything wrong. Wish you sympathize!
Thanks. Yes I was able to get broadcast receivers to work. I was however trying to point out the fact that ConnectivityChanged event in dotnet Maui doesn't work for Android 14.
The only hope we have now is that https://github.com/dotnet/maui/issues/19949 that you raised @mikeluken gets fixed.. and from what I can see is no part of any milestone.. which you know what that means
Adding the obligatory comment so I'll know when I can target Android 14
hi @jfversluis - Since Essentials is part of Maui now, is this issue going to be fixed? or a new issue should be created since this one is closed.
+1 to keep it on my radar
@TranTrieuLamQuynh
Thanks for the comment. However, I do not fully understand your post. I don't have a broadcast receiver to register. In App.xaml.cs, I am just specifying:
Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
In Android 13, this works. In Android 14,
Connectivity_ConnectivityChanged
is never hit when toggling on/off Wi-Fi.Can you please provide a full example of how you got this to work?
It appears as though a fix was already attempted for this:
However, I am running the latest nightly builds and this still does not work.
Note: I also added this to my MainActivity.OnCreate(...):
Intent.SetPackage(AppInfo.PackageName);
It had no effect.
@mikeluken Thank you! Can you please open a new issue for this? It'll be easier for us to track it. Thanks!!!
@TranTrieuLamQuynh Thanks for the comment. However, I do not fully understand your post. I don't have a broadcast receiver to register. In App.xaml.cs, I am just specifying:
Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
In Android 13, this works. In Android 14,
Connectivity_ConnectivityChanged
is never hit when toggling on/off Wi-Fi. Can you please provide a full example of how you got this to work? It appears as though a fix was already attempted for this: https://github.com/dotnet/maui/blame/584d99f14d9b54c25819d1594ceecd370183b92c/src/Essentials/src/Connectivity/Connectivity.android.cs#L48 However, I am running the latest nightly builds and this still does not work. Note: I also added this to my MainActivity.OnCreate(...):Intent.SetPackage(AppInfo.PackageName);
It had no effect.
@mikeluken Thank you! Can you please open a new issue for this? It'll be easier for us to track it. Thanks!!!
Just adding my two cents, this intent https://github.com/dotnet/maui/blob/977642b689f1af22afe0f4cbaf21b6d8106f648c/src/Essentials/src/Connectivity/Connectivity.android.cs#L18 needs to have package info set in order for the connectivity change event to fire because of this change by google: https://developer.android.com/about/versions/14/behavior-changes-14#safer-intents
If I set this in the constructor of that class, everything works fine:
connectivityIntent.SetPackage(AppInfo.PackageName);
Description
When adding a Connectivity.ConnectivityChanged handler, the following exception is thrown on an Android 14 device, when the targetSdkVersion is 34 (the default with new Maui app) an when run on Android 14:
Steps to Reproduce
Link to public reproduction project repository
No response
Version with bug
8.0.0-rc.1.9171
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android 14
Did you find any workaround?
Change the target Android version to 13. The code will work on Android 14 then.
Relevant log output