簡體   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