I have and application that it will start a foreground service if I enable a switch.
And this foreground service instantiate a class that it is needed to perform the logic of this foreground service.
The instantiation of the class it is performed in the OnCreate() method of the foreground service class, and this could thrown an expcetion if the class can't be instantiate because some data that is needed to perform.
I would like to propagate this exception to the caller of the service, to can handle, but how the exception is thrown in the OnCreate() method, the application exits without the possibility to handle the exception.
This is the code:
Foreground service:
[Service]
public class AlarmasForegroundService : Service
{
private IServicioAlarmas _servicioAlarmas;
private void StartForegroundService()
{
_notificationManager = (GetSystemService(Context.NotificationService) as NotificationManager)!;
createNotificationChannel(_notificationManager);
Notification notification = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
.SetContentTitle("GTS Registros Horarios")
.SetContentText($"{DateTime.Now}: Se han iniciado las alarmas.")
.SetSmallIcon(Resource.Drawable.logo32x32)
.SetAutoCancel(false)
.SetOngoing(false)
.Build();
StartForeground(NOTIFICATION_ID, notification);
}
private NotificationChannel? createNotificationChannel(NotificationManager notificationMnaManager)
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O) return null;
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationImportance.Default);
notificationMnaManager.CreateNotificationChannel(channel);
return channel;
}
public override IBinder OnBind(Intent? intent)
{
return null;
}
public override StartCommandResult OnStartCommand(Intent? intent, StartCommandFlags flags, int startId)
{
StartForegroundService();
_servicioAlarmas.Iniciar();
return StartCommandResult.Sticky;
}
#endregion creación del foreground
public override void OnCreate()
{
base.OnCreate();
//this can throw an exception, that i would like to propagete to be handle for the consumer that call the method Iniciar().
InicializarServicioAlarmas();
}
private void InicializarServicioAlarmas()
{
_servicioAlarmas = MauiApplication.Current.Services.GetService<IServicioAlarmas>()!;
IMessenger messenger = MauiApplication.Current.Services.GetService<IMessenger>()!;
messenger.Register<MensajeTexto>(this, (recipient, message) =>
{
ReceptorMensajes(message.Mensaje);
});
}
public void Iniciar()
{
var intent = new Intent(Android.App.Application.Context, typeof(AlarmasForegroundService));
Android.App.Application.Context.StartForegroundService(intent);
}
public void Detener()
{
_servicioAlarmas.DetenerAsync();
var intent = new Intent(Android.App.Application.Context, typeof(AlarmasForegroundService));
Android.App.Application.Context.StopService(intent);
}
}
This is the consumer that tries to start the foreground service. It is a view model of a view that has a swtich:
partial void OnAlarmasActivasChanged(bool value)
{
try
{
if (value == true)
{
_alarmasForegroundService.Iniciar();
}
else
{
_alarmasForegroundService.Detener();
}
}
catch (Exception ex)
{
//Here handle the exception that it si thrown in the method OnCreate() of the foreground service.
}
}
Is it possible to catch the exception in the view model to handle it?
Thanks.