diff --git a/DeviceTests/DeviceTests.Android/Properties/AndroidManifest.xml b/DeviceTests/DeviceTests.Android/Properties/AndroidManifest.xml index 814ca24c..23cb78ef 100644 --- a/DeviceTests/DeviceTests.Android/Properties/AndroidManifest.xml +++ b/DeviceTests/DeviceTests.Android/Properties/AndroidManifest.xml @@ -1,8 +1,8 @@  - - + + diff --git a/Xamarin.Essentials/Battery/Battery.android.cs b/Xamarin.Essentials/Battery/Battery.android.cs index 4c3bfedf..9e89388c 100644 --- a/Xamarin.Essentials/Battery/Battery.android.cs +++ b/Xamarin.Essentials/Battery/Battery.android.cs @@ -15,7 +15,7 @@ namespace Xamarin.Essentials return; powerReceiver = new EnergySaverBroadcastReceiver(OnEnergySaverChanged); - Platform.AppContext.RegisterReceiver(powerReceiver, new IntentFilter(PowerManager.ActionPowerSaveModeChanged)); + Platform.RegisterBroadcastReceiver(powerReceiver, new IntentFilter(PowerManager.ActionPowerSaveModeChanged), false); } static void StopEnergySaverListeners() @@ -52,7 +52,7 @@ namespace Xamarin.Essentials Permissions.EnsureDeclared(); batteryReceiver = new BatteryBroadcastReceiver(OnBatteryInfoChanged); - Platform.AppContext.RegisterReceiver(batteryReceiver, new IntentFilter(Intent.ActionBatteryChanged)); + Platform.RegisterBroadcastReceiver(batteryReceiver, new IntentFilter(Intent.ActionBatteryChanged), false); } static void StopBatteryListeners() @@ -76,7 +76,7 @@ namespace Xamarin.Essentials Permissions.EnsureDeclared(); using (var filter = new IntentFilter(Intent.ActionBatteryChanged)) - using (var battery = Platform.AppContext.RegisterReceiver(null, filter)) + using (var battery = Platform.RegisterBroadcastReceiver(null, filter, false)) { var level = battery.GetIntExtra(BatteryManager.ExtraLevel, -1); var scale = battery.GetIntExtra(BatteryManager.ExtraScale, -1); @@ -96,7 +96,7 @@ namespace Xamarin.Essentials Permissions.EnsureDeclared(); using (var filter = new IntentFilter(Intent.ActionBatteryChanged)) - using (var battery = Platform.AppContext.RegisterReceiver(null, filter)) + using (var battery = Platform.RegisterBroadcastReceiver(null, filter, false)) { var status = battery.GetIntExtra(BatteryManager.ExtraStatus, -1); switch (status) @@ -123,7 +123,7 @@ namespace Xamarin.Essentials Permissions.EnsureDeclared(); using (var filter = new IntentFilter(Intent.ActionBatteryChanged)) - using (var battery = Platform.AppContext.RegisterReceiver(null, filter)) + using (var battery = Platform.RegisterBroadcastReceiver(null, filter, false)) { var chargePlug = battery.GetIntExtra(BatteryManager.ExtraPlugged, -1); diff --git a/Xamarin.Essentials/Connectivity/Connectivity.android.cs b/Xamarin.Essentials/Connectivity/Connectivity.android.cs index 5cc92955..689732c0 100644 --- a/Xamarin.Essentials/Connectivity/Connectivity.android.cs +++ b/Xamarin.Essentials/Connectivity/Connectivity.android.cs @@ -34,7 +34,7 @@ namespace Xamarin.Essentials conectivityReceiver = new ConnectivityBroadcastReceiver(OnConnectivityChanged); - Platform.AppContext.RegisterReceiver(conectivityReceiver, filter); + Platform.RegisterBroadcastReceiver(conectivityReceiver, filter, false); } static void StopListeners() diff --git a/Xamarin.Essentials/Platform/Platform.android.cs b/Xamarin.Essentials/Platform/Platform.android.cs index 8a7053b1..4ad42323 100644 --- a/Xamarin.Essentials/Platform/Platform.android.cs +++ b/Xamarin.Essentials/Platform/Platform.android.cs @@ -122,6 +122,19 @@ namespace Xamarin.Essentials } } + internal static AndroidIntent RegisterBroadcastReceiver(BroadcastReceiver receiver, IntentFilter filter, bool exported) + { + if (HasApiLevel(34)) + { + var flags = exported ? ReceiverFlags.Exported : ReceiverFlags.NotExported; + + // Explicit cast of flags because of: https://github.com/xamarin/xamarin-android/issues/7503 + return AppContext.RegisterReceiver(receiver, filter, (ActivityFlags)flags); + } + + return AppContext.RegisterReceiver(receiver, filter); + } + internal static bool HasSystemFeature(string systemFeature) { var packageManager = AppContext.PackageManager;