How to give Reload in a Webviewer per time interval in Xamarin Android?

Asked

Viewed 86 times

0

I’m creating an app, where it has a Webview that needs to be updated every 5 seconds while the Switch is checked, and when you uncheck it, it should stop loading the page.

I tried to use a while, but the application is waiting for the while and during that it freezes, if you 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();
                    }
                };

1 answer

1

This strategy is not so performative, because it will, in fact lock the App, after all the main thread will be stuck in this while (Thread.Sleep(5);).

I recommend signing the event CheckedChange Switcher, every time it changes, will invoke that event. Something like

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

To upgrade every 5 seconds, within this event, you can use a 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 the Timer, dystopia: timer.Dispose(). This start and stop control is in charge of changing the e.IsChecked.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.