[英]WebView - load a new URL/File after X seconds, ad infinitum
I need to be able to load a URL/File in a WebView and then after X number of seconds, load another URL/File (eg stored in an array) 我需要能够在WebView中加载URL /文件,然后在X秒后加载另一个URL /文件(例如存储在数组中)
I can succesfully load web page 1, but when I put a Thread.sleep() call after the first .loadURL() method, followed by a new .loadURL() with a new file reference, running the app, the first file does not display, but jumps to the second. 我可以成功加载网页1,但是当我在第一个.loadURL()方法之后调用Thread.sleep(),然后是一个带有新文件引用的新.loadURL(),运行应用程序时,第一个文件执行不显示,但跳到第二个。
Code: 码:
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());
As you can see, this is not in a loop, as that is my other problem, I have no idea how to implement this into a loop (I at least wanted to get this bit working before I tackled the loop) 正如你所看到的,这不是一个循环,因为那是我的另一个问题,我不知道如何将它实现为循环(我至少想在解决循环之前让这一点工作)
Thanks! 谢谢!
You need to execute your code in a Thread or a Handler 您需要在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);
OR 要么
Thread t = new Thread() {
public void run() {
try {
//task 1...
Thread.sleep(1000);
//task 2...
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
};
t.start();
with a timer: 带有计时器:
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.