如何刷新Android的使用定时器的WebView? [英] How to refresh the WebView in android using a Timer?

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

问题描述

是否定时器可以被设置为刷新web视图每隔1分钟只有当应用程序当前是活动的

Whether a timer can be set to refresh the webview every 1 min only if the application is currently active?

无论是可能的吗?

推荐答案

首先,你需要创建一个的TimerTask 类:

First of all, you need to create a TimerTask class:

protected class ReloadWebView extends TimerTask {
    Activity context;
    Timer timer;
    WebView wv;

    public ReloadWebView(Activity context, int seconds, WebView wv) {
        this.context = context;
        this.wv = wv;

        timer = new Timer();
        /* execute the first task after seconds */
        timer.schedule(this,
                seconds * 1000,  // initial delay
                seconds * 1000); // subsequent rate

        /* if you want to execute the first task immediatly */
        /*
        timer.schedule(this,
                0,               // initial delay null
                seconds * 1000); // subsequent rate
        */
    }

    @Override
    public void run() {
        if(context == null || context.isFinishing()) {
            // Activity killed
            this.cancel();
            return;
        }

        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                wv.reload();
            }
        });
    }
}

在你的活动,您可以使用此行:

In your Activity, you can use this line:

new ReloadWebView(this, 60, wv);

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

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