C# -> Android (Xamarin) -> start task every 5 minutes in Background -


i want run task every 5 minutes. i've tried solve intentservice , alarmmanager, code:

protected override void oncreate(bundle bundle) {     base.oncreate(bundle);     setcontentview(resource.layout.main);      var tkrserviceintent = new intent(this, typeof(gpsdatahandler));     var tkrservicependingintent = pendingintent.getservice(this, 0, tkrserviceintent, 0);      long interval = 5000;     var firststart = (datetime.now.ticks / timespan.tickspermillisecond) + 1000;      var = (alarmmanager)getsystemservice(context.alarmservice);      am.setinexactrepeating(alarmtype.rtcwakeup, firststart, interval, tkrservicependingintent);      toast.maketext(this, "service started", toastlength.long).show(); } 

i receive toast, service started, if in running services, there no service application. can tell me problem ist?

intentservice in "activity" (if can call it) runing in background of app, finnally call ondestroy() .. can use timer fix problem , :

using system; using system.threading;  class timerexamplestate {     public int counter = 0;     public timer tmr; }  class app {    public static void main() {     timerexamplestate s = new timerexamplestate();      // create delegate invokes methods timer.     timercallback timerdelegate = new timercallback(checkstatus);      // create timer waits 1 second, invokes every second.     timer timer = new timer(timerdelegate, s, 1000, 1000);      // keep handle timer, can disposed.     s.tmr = timer;      // main thread nothing until timer disposed.     while (s.tmr != null)         thread.sleep(0);     console.writeline("timer example done.");    }    // following method called timer's delegate.     static void checkstatus(object state) {     timerexamplestate s = (timerexamplestate) state;     s.counter++;           console.writeline("{0} checking status {1}.",datetime.now.timeofday, s.counter);         if (s.counter == 5) {         // shorten period. wait 10 seconds restart timer.         (s.tmr).change(10000,100);         console.writeline("changed...");     }         if (s.counter == 10) {         console.writeline("disposing of timer...");         s.tmr.dispose();         s.tmr = null;     }    } } 

source : https://developer.xamarin.com/api/type/system.threading.timer/


Comments