Xevas
Xevas

Reputation: 13

Making Android WebView autorefresh periodically

I'm trying to create a hybrid application in Android that has a web view that's supposed to refresh every five minutes or so, or a few seconds after the user has done a change in the service. Most solutions that I've tried can be summarized into this SO question's answer. The problem is that everytime the application tries to refresh the web view through that, the software crashes because "an UI element has been called from non-UI thread".

Is there another alternative? I think I can do a periodical refresh through Javascript in the web page itself, but considering that our application has been designed to run even if there's no network and we can't then refresh it when user changes the status of the native side, it's not optimal. Does anyone have any suggestions?

Thanks in advance

Upvotes: 0

Views: 8480

Answers (1)

Eugene H
Eugene H

Reputation: 3568

Using a Toast as an example as how you can keep calling it. You can call the reload(); after a user changes information rather than the onCreate.

Load in the oncreate

Toast.makeText(LogThirdPager.this, "Hello", Toast.LENGTH_SHORT).show();
// mWebview.loadUrl("http://www.google.com");
reload();

Then call this after it. It will continue to reload the page every 5 seconds for example.

public void reload() {
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // Do something after 5s = 5000ms
            Toast.makeText(LogThirdPager.this, "Hello", Toast.LENGTH_SHORT).show();
            reload();
            // mWebview.loadUrl("http://www.google.com");
        }
    }, 5000);
}

Upvotes: 5

Related Questions