I retrieve some data from html after several step (login, click on link, run javascript, etc.) using webview, like described here: how to get html content from a webview? putting more actions in cascade. I don't use thread, mainly when an action (login for example) is done, it's called onPageFinished and this fire another webview.loadUrl and so on to another onPageFinished till all the actions are executed.
I want to create a widget that show this result, that fire this chain of webview.loadUrl when the widget refresh button is pressed or every day at 12AM. Only after that all the chain is completed, the result can be showed in the widget.
I've read some tutorials, and I saw that there're different approach, TimerTask, Service, Broadcast receiver, ect., but I didn't understand which fit to my project. I don't know if my explaination was clear, but in the onUpdate of the widget, I shoud start the webview.loadUrl sequence, but I can't return anything until the last onPageFinished is called.
This is what I mean for chain action, urlChain is a static string LinkedList with the sequence of URLs (and other stuff).
@SuppressLint("SetJavaScriptEnabled")
private void callChain()
{
/* WebViewClient must be set BEFORE calling loadUrl! */
browser.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
/* This call inject JavaScript into the page which just finished loading. */
if (!urlChain.isEmpty())
{
callChain();
}
}
});
/* load a web page */
if (!urlChain.isEmpty())
{
String currentUrl;
currentUrl = urlChain.pop();
browser.loadUrl(currentUrl);
}
}