I am trying to create a service that I could start and stop when I se a switch.
This is my code:
[Service]
public class MyForegroundService : Service
{
private string NOTIFICATION_CHANNEL_ID = "1000";
private int NOTIFICATION_ID = 1;
private string NOTIFICATION_CHANNEL_NAME = "notification";
private NotificationManager _notificationManager;
public MyForegroundService()
{
}
private void StartForegroundService()
{
_notificationManager = (GetSystemService(Context.NotificationService) as NotificationManager)!;
createNotificationChannel(_notificationManager);
Notification notification = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
.SetAutoCancel(false)
.SetOngoing(true)
.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)
{
if(intent.Action == "START_SERVICE")
{
StartForegroundService();
}
else if(intent.Action == "STOP_SERVICE")
{
StopForeground(true);
StopSelfResult(startId);
}
return StartCommandResult.NotSticky;
}
public void Start()
{
Intent intent = new Intent(Android.App.Application.Context, typeof(MyForegroundService));
intent.SetAction("START_SERVICE");
Android.App.Application.Context.StartForegroundService(intent);
}
public void STOP()
{
Intent intent = new Intent(Android.App.Application.Context, typeof(MyForegroundService));
intent.SetAction("STOP_SERVICE");
Android.App.Application.Context.StartForegroundService(intent);
}
}
I have realize that I can start and stop the foreground service if I do it in the OnStartCommand(). If I try to do it outside, it doesn't work.
For this reason, the implementation of my Stop() method is this:
public void STOP()
{
Intent intent = new Intent(Android.App.Application.Context, typeof(MyForegroundService));
intent.SetAction("STOP_SERVICE");
Android.App.Application.Context.StartForegroundService(intent);
}
The problem with this code, is that I am using the StartForegroundService() to force to execute the code in OnStartCommand(). But It looks like a bad workaround, because StartForegroundService it should be to use to start the foreground, not to stop it.
However, I try anothers implmentantions of the Stop() method, but no one of these works. Something like this:
public void STOP()
{
var intent = new Intent(Android.App.Application.Context, this.Class);
intent.SetAction("STOP_SERVICE");
Android.App.Application.Context.StopService(intent);
}
I have try an example application that I found in youtube that it works. The implementation is this:
public void Stop()
{
Intent stopIntent = new Intent(MainActivity.ActivityCurrent, this.Class);
stopIntent.SetAction("STOP_SERVICE");
MainActivity.ActivityCurrent.StartService(stopIntent);
}
But in my case, MainActivity.ActivityCurrent is not available. Also I am not the reason to create the intent in this way. Why is it used the MainActivity of Android? Wouldn't it better to use the application context instead? Which is the difference?
So in summary, I would like to know how I should Stop the service.
Thanks so much.
I created a new project to test starting and stopping the foreground service in the ContentPage's button clicked event in the maui. You can use the Android.App.Application.Context.StopService()
to stop the foreground service.
Start the foreground service:
private void StartButton_Clicked(object sender, EventArgs e)
{
#if ANDROID
Android.Content.Intent intent = new Android.Content.Intent(Android.App.Application.Context,typeof(ForegroundServiceDemo));
Android.App.Application.Context.StartForegroundService(intent);
#endif
}
And the Stop button:
private void StopButton_Clicked(object sender, EventArgs e)
{
#if ANDROID
Android.Content.Intent intent = new Android.Content.Intent(Android.App.Application.Context, typeof(ForegroundServiceDemo));
Android.App.Application.Context.StopService(intent);
#endif
}