Hello,
Welcome to our Microsoft Q&A platform!
Try using the Handler
with an Action
to keep doing the work in a foreground service. Here is the sample code, you could refer to:
[Service(Enabled = true)]
public class TestService : Service
{
private Handler handler;
private Action runnable;
private bool IsStarted;
private int DELAY_BETWEEN_LOG_MESSAGES = 5000;
private int NOTIFICATION_SERVICE_ID = 1001;
private int NOTIFICATION_AlARM_ID = 1002;
private string NOTIFICATION_CHANNEL_ID = "1003";
private string NOTIFICATION_CHANNEL_NAME = "MyChannel";
public override void OnCreate()
{
base.OnCreate();
handler = new Handler(Application.MainLooper);
runnable = new Action(() =>
{
if (IsStarted)
{
functionMethod();
handler.PostDelayed(runnable, DELAY_BETWEEN_LOG_MESSAGES);
}
});
}
private void functionMethod()
{
//
}
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
if (IsStarted)
{
//service is already started
}
else
{
CreateNotificationChannel();
DispatchNotification_RegisterService();//register a service as a foreground service
handler.PostDelayed(runnable, DELAY_BETWEEN_LOG_MESSAGES);
IsStarted = true;
}
return StartCommandResult.Sticky;
}
public override void OnDestroy()
{
// Stop the handler.
handler.RemoveCallbacks(runnable);
// Remove the notification from the status bar.
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Cancel(NOTIFICATION_SERVICE_ID);
IsStarted = false;
base.OnDestroy();
}
public override IBinder OnBind(Intent intent)
{
return null;
}
private void DispatchNotification_RegisterService()
{
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
if (Build.VERSION.SdkInt > BuildVersionCodes.O)
{
var chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, GetString(Resource.String.abc_action_bar_home_description), NotificationImportance.Default);
chan.LightColor = Android.Graphics.Color.Green;
chan.LockscreenVisibility = NotificationVisibility.Private;
notificationManager.CreateNotificationChannel(chan);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.SetAutoCancel(false)
.SetSmallIcon(Resource.Drawable.abc_btn_radio_material)
.SetContentTitle("Mobile")
.SetContentText("My service started");
StartForeground(NOTIFICATION_SERVICE_ID, builder.Build());
}
else
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.SetDefaults((int)NotificationDefaults.All)
.SetSmallIcon(Resource.Drawable.abc_btn_radio_material)
.SetVibrate(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 })
.SetSound(null)
.SetPriority(NotificationCompat.PriorityDefault)
.SetAutoCancel(false)
.SetContentTitle("Mobile")
.SetContentText("My service started")
.SetOngoing(true);
StartForeground(NOTIFICATION_SERVICE_ID, builder.Build());
}
}
private void CreateNotificationChannel()
{
//Notification Channel
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationImportance.Max);
notificationChannel.EnableLights(true);
notificationChannel.EnableVibration(true);
notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 });
NotificationManager notificationManager = (NotificationManager)this.GetSystemService(Context.NotificationService);
notificationManager.CreateNotificationChannel(notificationChannel);
}
}
Best Regards,
Jarvan Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.