Я пытаюсь создать приложение, которое частично работает. Когда я тестирую на Android 12, эта строка StartForeground(1, Notification); правильно запускает Foreground.
но на Android 14 у меня все еще есть ошибка в StartForeground(1, Notification,foregroundServiceType); : Java.Lang.IllegalArgumentException: foregroundServiceType 0x00000001 не является подмножеством атрибута foregroundServiceType 0x00000000 в служебном элементе файла манифеста ---> Android.OS.RemoteException: Remote трассировка стека: com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:2053)
или в StartForeground(1, Notification); : Android.App.MissingForegroundServiceTypeException: 'Запуск FGS без типа callerApp=ProcessRecord{f13ab95 8713:com.companyname.mazurek/u0a193} targetSDK=34'
это полный код сервиса:
Код: Выделить всё
using Android.App;
using MyContent = Android.Content ;
using Android.Content;
using Android.OS;
namespace Mazurek.Platforms.Android
{
[Service]
public class MainQueryBackgroundService : Service
{
private CancellationTokenSource _cancellationTokenSource;
private static bool _isServiceRunning = false;
public override IBinder OnBind(Intent intent)
{
return null;
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
if (_isServiceRunning)
{
return StartCommandResult.Sticky;
}
_isServiceRunning = true;
var notification = new Notification.Builder(this, "MyNotificationChannel")
.SetContentTitle("MainQuery CDM")
.SetContentText("Serwis MainQuery działa w tle...")
//.SetSmallIcon(Resource.Drawable.ic_notification)
.Build();
try
{
var foregroundServiceType = MyContent.PM.ForegroundService.TypeDataSync;
StartForeground(1, notification, foregroundServiceType);
}
catch (Exception ex)
{
StartForeground(1, notification);
}
_cancellationTokenSource = new CancellationTokenSource();
Task.Run(async () =>
{
try
{
await DoWorkInBackground(_cancellationTokenSource.Token);
}
catch
{
}
});
return StartCommandResult.Sticky;
}
private async Task DoWorkInBackground(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
BackgroundWork.MainQueryTasks tasks = new BackgroundWork.MainQueryTasks();
await tasks.MainQuery();
await Task.Delay(60 * 1000);
}
}
public override void OnDestroy()
{
_isServiceRunning = false;
_cancellationTokenSource?.Cancel();
base.OnDestroy();
}
}
}
Код: Выделить всё
Код: Выделить всё
net8.0-android;net8.0-maccatalyst;net8.0-ios
Exe
Mazurek
true
true
enable
enable
Mazurek
com.companyname.mazurek
1.0
1
10.0
13.1
31.0
10.0.17763.0
10.0.17763.0
6.5
apk
True
$(TargetFrameworks);net8.0-windows10.0.19041.0
PreserveNewest
Подробнее здесь: https://stackoverflow.com/questions/790 ... roid-12-14
Мобильная версия