Hello,
First, sorry for this question, there is a lot of subjects for this point but, the sample project not working anymore, or it's only xamarin.android, or I am to dumb I don't know ..
I am pretty new to Xamarin, but I readded all the Microsoft docs about Services, locale notification, foreground service.
But I am not able to create a running app.
To start, I just want to create an app that count seconds between start and stop service, so, I will like to know if you have a simple project starting and stopping a foreground service. As simple as it's can be, my goal it's to understand how it's working
I will continue to work on my side to achieve this goal, creating a new project, restart from nothing and hope I will success, as it seem to be a simple thing ..
Thanks for your help and your time !
Edit : I created a simple project, still not working ...
In cross code
MainPage.xaml
`private void startClicked(object sender, EventArgs e)
{
try
{
//Start service
DependencyService.Get<IAndroidService>().StartService();
}
catch
{
return;
}
startBtn.IsEnabled = false;
stopBtn.IsEnabled = true;
}
private void stopClicked(object sender, EventArgs e)
{
try
{
//Stop service
DependencyService.Get<IAndroidService>().StopService();
}
catch
{
return;
}
startBtn.IsEnabled = true;
stopBtn.IsEnabled = false;
}`
IAndroidService.cs
`public interface IAndroidService
{
void StartService();
void StopService();
}`
INotification
public interface INotification { Notification ReturnNotif(); }
In Android code
AndroidServiceHelper.cs
`internal class AndroidServiceHelper : IAndroidService
{
private static Context context = global::Android.App.Application.Context;
public void StartService()
{
var intent = new Intent(context, typeof(simpleService));
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
context.StartForegroundService(intent);
}
else
{
context.StartService(intent);
}
}
public void StopService()
{
var intent = new Intent(context, typeof(simpleService));
context.StopService(intent);
}
}`
in NotificationHelper.cs
` internal class NotificationHelper : INotification
{
private static string foregroundChannelId = "1003";
private static Context context = global::Android.App.Application.Context;
public Notification ReturnNotif()
{
// Building intent
var intent = new Intent(context, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
intent.PutExtra("Simple project", "Message from simple project");
var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.UpdateCurrent);
var notifBuilder = new NotificationCompat.Builder(context, foregroundChannelId)
.SetContentTitle("Simple project")
.SetContentText("Main text for simple project")
.SetSmallIcon(Resource.Drawable.LogoBenne)
.SetOngoing(true)
.SetContentIntent(pendingIntent);
// Building channel if API verion is 26 or above
if (global::Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
NotificationChannel notificationChannel = new NotificationChannel(foregroundChannelId, "TitleSimpleProject", NotificationImportance.High);
notificationChannel.Importance = NotificationImportance.High;
notificationChannel.EnableLights(true);
notificationChannel.EnableVibration(true);
notificationChannel.SetShowBadge(true);
notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 });
var notifManager = context.GetSystemService(Context.NotificationService) as NotificationManager;
if (notifManager != null)
{
notifBuilder.SetChannelId(foregroundChannelId);
notifManager.CreateNotificationChannel(notificationChannel);
}
}
return notifBuilder.Build();
}
}`
in simpleService.cs
`public class simpleService : Service
{
private int number = 0;
public override IBinder OnBind(Intent intent)
{
return null;
}
public const int ServiceRunningNotifID = 9000;
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
Notification notif = DependencyService.Get<INotification>().ReturnNotif();
StartForeground(ServiceRunningNotifID, notif);
count(ref number);
return StartCommandResult.Sticky;
}
public override void OnDestroy()
{
base.OnDestroy();
}
public override bool StopService(Intent name)
{
return base.StopService(name);
}
private void count(ref int number)
{
number++;
}
}`
Thanks a lot !