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)
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.
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
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());
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.