I need to run a method every 5 seconds using Mono for Android. Is there a scheduled timer in Android? I have tried this code, however, it fails to start:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Glass.Core.Interfaces;
using Glass.Core;
using Java.Util;
namespace Glass.UI.AN
{
[Application(Label = "Glass", Icon = "@drawable/icon")]
public class GlassApplication : Application
{
Context context;
public GlassApplication (IntPtr handle, JniHandleOwnership transfer)
: base(handle, transfer)
{
this.context = BaseContext;
}
public override void OnCreate ()
{
base.OnCreate ();
Timer timer = new Timer ();
timer.ScheduleAtFixedRate (new CustomTimerTask(context), new Date(DateTime.Now.Year, DateTime.Now.Month,
DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute), 5000);
}
}
public class CustomTimerTask: TimerTask
{
Context context;
public CustomTimerTask(Context context)
{
this.context = context;
}
public override void Run ()
{
GlassWebServiceProvider p = new GlassWebServiceProvider (context);
p.SendCardReaders ();
}
}
}
Why not just use
System.Timers.Timer
?Another approach to make a new
Task
orThread
and let it sleep for 5 seconds every time:I had the same problem, I had to start a service after a certain time.