Auto Refresh in Xamarin.forms application from time to time

3

I'm developing an application that on the main screen will have a few buttons, and these buttons access a URL sending commands on and off all through HttpClient and persisting all this information.

I have the following scenario:

  • I have 3 buttons all with status off.
  • User 1 with your cell phone clicks the button 3 that accesses the server and this information persists for connected
  • Mobile User 2 should from time to time know the status of these buttons
  • Both Users will be able to use the on and off buttons.

How to do the activity from time to time to perform this update, I tried to do the example below but I succeeded.

I'm waiting for you.

Handler handler = new Handler();

private void doTheAutoRefresh()
        {
            handler.postDelayed(new Runnable() {
        @Override
        public void run()
        {
            // Write code for your refresh logic
            doTheAutoRefresh();
        }
    }, 5000);
}
    
asked by anonymous 29.03.2017 / 22:55

2 answers

2

Friends solved the problem and to better detail and share what I could do, I had to answer my own question and not only to comment

Imported the library: using Android.OS;

Initialize Handler: Handler handler = new Handler ();

I have a function that initializes the buttons that are on the screen:

InicializaButtons()
{
    //Realiza os procedimentos de inicialização dos botões

    //Esta função não só inicializa, mas é usada varias vezes no decorrer da utilização do software, quando necessário.


    //Aqui esta a mágica do negócio
    //De 5 em 5 segundos estou chamando a função InicializaButtons
    handler.PostDelayed(InicializaButtons, 5000);
}

I hope to help many people with this solution.

I am testing here to see the performance of the device, the already seen we are dealing with mobile devices with hardware most of the time a little weak.

    
03.04.2017 / 04:13
0

The Android.OS library solution will probably resolve in cases where Xamarin.Droid is used, but since the raised question is about Xamarin.Forms in order to perform a task within a given range, it is necessary to use of Timers, follows the link of a test to see the behavior of a timer in Xamarin.Forms. Timestamp test code in Xamarin.Forms

    
05.04.2017 / 18:36