使 Android WebView 定期自动刷新 [英] Making Android WebView autorefresh periodically

查看:63
本文介绍了使 Android WebView 定期自动刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Android 中创建一个混合应用程序,该应用程序的 Web 视图应该每五分钟左右刷新一次,或者在用户更改服务后几秒钟刷新一次.我尝试过的大多数解决方案都可以总结为 thisSO 问题的答案. 问题是每次应用程序尝试通过它刷新 Web 视图时,软件都会崩溃,因为从非 UI 线程调用了一个 UI 元素".

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".

还有其他选择吗?我想我可以通过网页本身的 Javascript 进行定期刷新,但是考虑到我们的应用程序被设计为即使没有网络也可以运行,并且当用户更改本机端的状态时我们无法刷新它,这是不是最优的.有人有什么建议吗?

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?

提前致谢

推荐答案

以 Toast 为例,说明如何继续调用它.你可以调用 reload();在用户更改信息而不是 onCreate 之后.

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.

在oncreate中加载

Load in the oncreate

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

然后在它之后调用它.例如,它将继续每 5 秒重新加载一次页面.

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);
}

这篇关于使 Android WebView 定期自动刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆