using Android.App; using Android.Content; using Android.OS; using AndroidX.Core.App; namespace LatestMobilePrices.Platforms.Android { [Service] public class ForegroundServiceDemo : Service { private string NOTIFICATION_CHANNEL_ID = "1000"; private int NOTIFICATION_ID = 1; private string NOTIFICATION_CHANNEL_NAME = "notification"; private void startForegroundService() { var notifcationManager = GetSystemService(Context.NotificationService) as NotificationManager; if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { createNotificationChannel(notifcationManager); } var notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); notification.SetAutoCancel(false); notification.SetOngoing(true); notification.SetSmallIcon(Resource.Mipmap.appicon); notification.SetContentTitle("ForegroundService"); notification.SetContentText("Foreground Service is running"); StartForeground(NOTIFICATION_ID, notification.Build()); } private 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.NotSticky; } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)