繁体   English   中英

WebView - 在X秒后加载新的URL /文件,无限制

[英]WebView - load a new URL/File after X seconds, ad infinitum

我需要能够在WebView中加载URL /文件,然后在X秒后加载另一个URL /文件(例如存储在数组中)

我可以成功加载网页1,但是当我在第一个.loadURL()方法之后调用Thread.sleep(),然后是一个带有新文件引用的新.loadURL(),运行应用程序时,第一个文件执行不显示,但跳到第二个。

码:

file = new File(file_1.html");
webView.loadUrl("file:///" + file.getAbsolutePath());

try {
    Thread.sleep(1000*10); //  10 seconds
} catch (InterruptedException e) {
    e.printStackTrace();
}

file = new File(file_2.html");
webView.loadUrl("file:///" + file.getAbsolutePath());

正如你所看到的,这不是一个循环,因为那是我的另一个问题,我不知道如何将它实现为循环(我至少想在解决循环之前让这一点工作)

谢谢!

您需要在Thread或Handler中执行代码

file = new File(file_1.html");
webView.loadUrl("file:///" + file.getAbsolutePath());

new Handler().postDelayed(new Runnable() {

            public void run() {

                file = new File(file_2.html");
                webView.loadUrl("file:///" + file.getAbsolutePath());
            }

        }, 1000);

要么

Thread t = new Thread() {
            public void run() {
                try {
                    //task 1...
                    Thread.sleep(1000);
                    //task 2...
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

            }
        }
};

t.start();

带有计时器:

timer = new Timer();
timer.schedule(new MyTask(), 0, 5000);

class MyTask extends TimerTask {

        @Override
        public void run() {
            file = new File("file_1.html");
            webView.loadUrl("file:///" + file.getAbsolutePath());

            try {
                Thread.sleep(1000 * 10); // 10 seconds
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            file = new File("file_2.html");
            webView.loadUrl("file:///" + file.getAbsolutePath());

        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM