How to give reload in a WebViewer by time interval in Xamarin Android?

0

I'm creating an application, where you have a WebView that needs to be updated from 5 in 5 seconds while the Switch is checked, and when you uncheck it, it should do the page stops loading.

I tried to use a while, but the application is waiting while while it freezes, if they can help me with this problem I will be very grateful!

My current code:

nswtURL.Click += delegate
                {
                    if (nswtURL.Checked)
                    {
                        //true
                        nWebView.LoadUrl(URL);
                        while(nswtCamera.Checked)
                        {
                            nWebView.Reload();
                            Thread.Sleep(5);
                        }
                    }
                    else
                    {
                        //false
                        nWebView.StopLoading();
                    }
                };
    
asked by anonymous 01.11.2016 / 22:54

1 answer

1

This strategy is not as performative as this will actually crash the App, after all the main thread will be locked in this while (Thread.Sleep(5);) .

I recommend that you sign the CheckedChange event of the switcher, every time it changes, it will invoke this event. Something like

nswtURL.CheckedChange += CheckedChangeEvent;
void CheckedChangeEvent(object sender, CompoundButton.CheckedChangeEventArgs e)
{
    //Verificar o e.IsChecked
}

To refresh every 5 seconds within this event, you can use System.Threading.Timer . Something like:

timer = new Timer(new TimerCallback((o) => {
    nWebView.Reload();
    RunOnUiThread(() => {
        //aqui você pode atualizar sua tela
        nWebView.Reload();
    });
}), null, 5000, 5000);

To stop Timer , give it a dispose: timer.Dispose() . This start and stop control is in charge of changing e.IsChecked .

Just be careful because these methods will certainly consume a lot of battery power. You can add a strategy to let the user refresh the screen. Of course each case is a case, but for sure the battery will be drained here: /

    
13.03.2017 / 20:30