Buat Interface “IDemoService” di Xamarin.
using System;
using System.Collections.Generic;
using System.Text;
namespace ForegroundXamarin
{
public interface IDemoServices
{
void Start();
void Stop();
}
}
Buat Class “DemoService” di bagian Xamarin.Droid:
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using AndroidApp = Android.App.Application;
[assembly:Xamarin.Forms.Dependency(typeof(ForegroundXamarin.Droid.DemoServices))]
namespace ForegroundXamarin.Droid
{
[Service(ForegroundServiceType = Android.Content.PM.ForegroundService.TypeDataSync)]
public class DemoServices : Service, IDemoServices
{
public override IBinder OnBind(Intent intent)
{
throw new NotImplementedException();
}
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
if(intent.Action == "START_SERVICE")
{
RegisterNotification();
DoSomething();
}
else if (intent.Action == "STOP_SERVICE")
{
StopForeground(true);
StopSelfResult(startId);
}
return StartCommandResult.NotSticky;
}
int i = 1;
void DoSomething()
{
Device.StartTimer(TimeSpan.FromSeconds(10), () =>
{
NotificationManager manager = (NotificationManager)MainActivity.ActivityCurrent.
GetSystemService(Context.NotificationService);
// if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
// {
// var channelNameJava = new Java.Lang.String("Default");
// var channel = new NotificationChannel("default", channelNameJava, NotificationImportance.Default)
// {
// Description = "Channel Description..."
// };
// manager.CreateNotificationChannel(channel);
// }
Notification notification = new Notification.Builder(this, "ServiceChannel")
.SetContentTitle("My Foreground Service : " + i.ToString())
.SetSmallIcon(Resource.Drawable.abc_ic_star_black_16dp)
.SetOngoing(true)
.Build();
manager.Notify(100, notification);
i += 1;
return true;
});
}
private void RegisterNotification()
{
//NotificationChannel channel = new NotificationChannel("ServiceChannel", "Demo Foreground Service",
// NotificationImportance.Max);
//NotificationManager manager = (NotificationManager)MainActivity.ActivityCurrent.
// GetSystemService(Context.NotificationService);
//manager.CreateNotificationChannel(channel);
//Notification notification = new Notification.Builder(this, "ServiceChannel")
// .SetContentTitle("My Foreground Service")
// .SetSmallIcon(Resource.Drawable.abc_ic_star_black_16dp)
// .SetOngoing(true)
// .Build();
NotificationManager manager = (NotificationManager)MainActivity.ActivityCurrent.
GetSystemService(Context.NotificationService);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var channelNameJava = new Java.Lang.String("Default");
var channel = new NotificationChannel("default", channelNameJava, NotificationImportance.Default)
{
Description = "Channel Description..."
};
manager.CreateNotificationChannel(channel);
}
Notification notification = new Notification.Builder(this, "ServiceChannel")
.SetContentTitle("My Foreground Service")
.SetSmallIcon(Resource.Drawable.abc_ic_star_black_16dp)
.SetOngoing(true)
.Build();
StartForeground(100, notification);
}
public void Start()
{
Intent startService = new Intent(MainActivity.ActivityCurrent, typeof(DemoServices));
startService.SetAction("START_SERVICE");
MainActivity.ActivityCurrent.StartService(startService);
}
public void Stop()
{
Intent stopIntent = new Intent(MainActivity.ActivityCurrent, this.Class);
stopIntent.SetAction("STOP_SERVICE");
MainActivity.ActivityCurrent.StartService(stopIntent);
}
}
}
Di bagian Xamarin.Droid MainActivity.cs tambah beberapa kode berikut:
public static Activity ActivityCurrent { get; private set; }
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
ActivityCurrent = this;
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}