-
-
Save norton287/bda56cf4b5a55b3729bdd0222381ab18 to your computer and use it in GitHub Desktop.
How To Do A .NET MAUI Foreground Service For Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Android.App; | |
using Android.Content; | |
using Android.OS; | |
using AndroidX.Core.App; | |
namespace YourMAUIApp; | |
[Service] | |
public partial class MyForeGroundService : Service, IService | |
{ | |
public const string NOTIFICATION_CHANNEL_ID = "10276"; | |
private const int NOTIFICATION_ID = 10923; | |
private const string NOTIFICATION_CHANNEL_NAME = "notification"; | |
private void StartForegroundService() | |
{ | |
var intent = new Intent(Android.App.Application.Context, typeof(MainActivity)); | |
var pendingIntentFlags = Build.VERSION.SdkInt >= BuildVersionCodes.S | |
? PendingIntentFlags.UpdateCurrent | | |
PendingIntentFlags.Mutable | |
: PendingIntentFlags.UpdateCurrent; | |
var pendingIntent = PendingIntent.GetActivity(Android.App.Application.Context, 0, intent, pendingIntentFlags); | |
var notifcationManager = GetSystemService(NotificationService) as NotificationManager; | |
if (Build.VERSION.SdkInt >= BuildVersionCodes.O) | |
{ | |
CreateNotificationChannel(notifcationManager); | |
} | |
var notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); | |
notification.SetContentIntent(pendingIntent); | |
notification.SetAutoCancel(false); | |
notification.SetOngoing(true); | |
notification.SetSmallIcon(Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu ? Resource.Drawable.ic_notification : Resource.Mipmap.appicon); | |
notification.SetContentTitle("My Aoo"); | |
notification.SetContentText("My App Service is running"); | |
StartForeground(NOTIFICATION_ID, notification.Build()); | |
} | |
private static void CreateNotificationChannel(NotificationManager notificationMnaManager) | |
{ | |
var channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, | |
NotificationImportance.Low); | |
notificationMnaManager.CreateNotificationChannel(channel); | |
} | |
public override IBinder OnBind(Intent intent) | |
{ | |
return null; | |
} | |
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) | |
{ | |
StartForegroundService(); | |
return StartCommandResult.Sticky; | |
} | |
public void Start() | |
{ | |
var intent = new Intent(Android.App.Application.Context, typeof(MyForeGroundService)); | |
Android.App.Application.Context.StartForegroundService(intent); | |
} | |
public void Stop() | |
{ | |
var intent = new Intent(Android.App.Application.Context, typeof(MyForeGroundService)); | |
Android.App.Application.Context.StopService(intent); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace YourMAUIApp; | |
public partial class HomePage : ContentPage | |
{ | |
public readonly HomeViewModel ViewModel; | |
public IService Service { get; } | |
public HomePage(IService _service) | |
{ | |
InitializeComponent(); | |
ViewModel = new HomeViewModel(); | |
Service = _service; | |
} | |
private void Button_OnClicked(object sender, EventArgs e) | |
{ | |
Service.Start(); | |
} | |
private void StopButton_OnClicked(object sender, EventArgs e) | |
{ | |
Service.Stop(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace YourMAUIApp; | |
public interface IService | |
{ | |
void Start(); | |
void Stop(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace YourMAUIApp; | |
public static class MauiProgram | |
{ | |
public static IServiceProvider Services { get; private set; } | |
public static MauiApp CreateMauiApp() | |
{ | |
var builder = MauiApp.CreateBuilder(); | |
builder.Services.AddTransient<IService, MyForeGroundService>(); | |
builder.Services.AddTransient<HomePage>(); | |
builder | |
.UseMauiApp<App>() | |
.UseMauiCommunityToolkit() | |
.UseMauiCommunityToolkitCore() | |
.UseMauiCommunityToolkitMarkup() | |
.ConfigureFonts(fonts => | |
{ | |
fonts.AddFont("Roboto-Regular.ttf", "RobotoRegular"); | |
}) | |
var app = builder.Build(); | |
Services = app.Services; | |
return app; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment