How to run code after delay in Xamarin Android

I am trying to show code after a delay in an android app.
The Java code for this looks something like this:

new Handler().postDelayed(new Runnable() { @Override public void run() { // your code that you want to delay here } }, 1000/* 1000ms = 1sec delay */); 

How to do this in Xamarin.Android with C #?

+5
source share
2 answers

You can try the following:

 Handler h = new Handler(); Action myAction = () => { // your code that you want to delay here }; h.PostDelayed(myAction, 1000); 

Take a look at the document

+13
source

I recommend using a cross-platform timer such as AdvancedTimer . Check out: github repo

API usage

To access the Timer class, simply use the dependency service:

 IAdvancedTimer timer = DependencyService.Get<IAdvancedTimer>(); 

YOU SHOULD call initTimer to initialize the timer;

 timer.initTimer(3000, timerElapsed, true); initTimer(interval, Eventhandler function, AutoReset); 

Methods

 timer.startTimer(); timer.stopTimer(); timer.getInterval() timer.setInterval(5000); timer.isTimerEnabled(); 
+1
source

All Articles