Merge pull request #2108 from xamarin/api34-compat
Add API 34/Android 14 compatibility for Connectivity & Battery
This commit is contained in:
Коммит
6ec2124ec4
|
@ -1,8 +1,8 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0.1.0" package="com.xamarin.essentials.devicetests" android:installLocation="auto">
|
||||
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="33" />
|
||||
<uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS" />
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS" />
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.BATTERY_STATS" />
|
||||
|
|
|
@ -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<Permissions.Battery>();
|
||||
|
||||
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<Permissions.Battery>();
|
||||
|
||||
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<Permissions.Battery>();
|
||||
|
||||
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<Permissions.Battery>();
|
||||
|
||||
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);
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ namespace Xamarin.Essentials
|
|||
|
||||
conectivityReceiver = new ConnectivityBroadcastReceiver(OnConnectivityChanged);
|
||||
|
||||
Platform.AppContext.RegisterReceiver(conectivityReceiver, filter);
|
||||
Platform.RegisterBroadcastReceiver(conectivityReceiver, filter, false);
|
||||
}
|
||||
|
||||
static void StopListeners()
|
||||
|
|
|
@ -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;
|
||||
|
|
Загрузка…
Ссылка в новой задаче